Given a binary tree
struct Treelinknode { treelinknode *left; Treelinknode *right; Treelinknode *next; }
Populate each of the next pointer to the next right node. If There is no next right node, the next pointer should are set to NULL .
Initially, all next pointers is set to NULL .
Note:
- Constant extra space.
- You could assume that it was a perfect binary tree (ie, all leaves was at the same level, and every parent had both children).
For example,
Given the following perfect binary tree,
1 / 2 3 /\ / 4 5 6 7
After calling your function, the tree is should look like:
1, NULL / 2, 3, null /\ / 4->5->6->7, NULL
The requirement of the first question is a complete binary tree, the second question does not have this requirement. It is clear that if there is no space complexity requirements, O (1), the problem can be done in accordance with the idea of hierarchical traversal, but also refers to the concept of tool methods, you can first look at this problem: http://blog.csdn.net/my_jobs/article/details/47665089 by the level of traversal. Can completely transform this problem. But both of these questions require a space complexity of O (1). So this method is not possible, but the code can be given, the two questions are completely generic:
public void Connect (Treelinknode root) { if (root = null) return; linkedlist<treelinknode> queue = new linkedlist<> (); Queue.add (root); while (!queue.isempty ()) { int len = Queue.size (); Treelinknode pre = null; for (int i = 0; i < len; i++) { treelinknode node = Queue.poll (); if (i = = 0) { Pre = node; } else { pre.next = node; Pre = Pre.next; } if (node.left! = null) Queue.add (node.left); if (node.right! = null) Queue.add (node.right);}}}
It can be seen that the above code is completely transformed out, so some common algorithms, must be familiar with the heart.
First of all, better to do. Look at the code to understand:
public void Connect (Treelinknode root) { if (root = null) return; Root.next = null; while (root.left! = null) { Treelinknode p = root; while (P! = null) { p.left.next = p.right; if (p.next! = null) { p.right.next = p.next.left; } else { p.right.next = null; } p = p.next; } root = Root.next; } }
The essence of this is that at the level of iteration, it is important to make effective use of what has been built, such as the next pointer.
In the second question, the second question is difficult.
public void Connect (Treelinknode root) { while (root = null) { Treelinknode childhead = new Treelinknode (0); Treelinknode p = childhead; while (root = null) { if (root.left! = null) { p.next = root.left; p = p.next; } if (root.right! = null) { p.next = root.right; p = p.next; } root = Root.next; } P.next = null; root = Childhead.next; } }
This question compares the second is each layer of the head pointer, too ingenious. The use of linked list Properties!!!
Transformation questions:
Given The following binary tree,
1 / 2 3 \ / 5 6 7
After calling your function, the tree is should look like:
1, NULL / 2, 3, null / 5->6->7, NULL
The data structure of the tree is unchanged, if the current node is the first node of this layer, the left pointer points to the lower-level first node, otherwise it is empty. The right pointer points to the next node in the layer. The universal solution, the hierarchical traversal:
Public TreeNode treetolist (TreeNode root) { if (root = null) return null; linkedlist<treenode> queue = new linkedlist<treenode> (); Queue.add (root); TreeNode head = new TreeNode (0); while (!queue.isempty ()) { int len = Queue.size (); TreeNode pre = null; for (int i = 0; i < len; i++) { TreeNode node = Queue.poll (); if (node.left! = null) Queue.add (node.left); if (node.right! = null) Queue.add (node.right); Node.left = null; Node.right = null; if (i = = 0) { Pre = node; Head.left = pre; head = Head.left; } else { pre.right = node; Pre = Pre.right; }}} return root; }
Compared to the above two questions have next pointer can be used, this has no, must first the lower nodes are saved to modify the node relationship of this layer!!!
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Leetcode-populating Next right pointers on each Node II and its variants