Question
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
Solution 1--BFS
Key to the solution are to traverse tree level by level. Therefore, we can use BFS. Time complexity O (n), n is the number of nodes, and space cost is O (n).
1 /**2 * Definition for binary tree with next pointer.3 * public class Treelinknode {4 * int val;5 * Treelinknode left, right, next;6 * Treelinknode (int x) {val = x;}7 * }8 */9 Public classSolution {Ten Public voidConnect (Treelinknode root) { One //We can use BFS to solve this problem AList<treelinknode> current =NewArraylist<treelinknode>(); -List<treelinknode>Next; - if(Root = =NULL) the return; - Current.add (root); - while(Current.size () > 0) { -Next =NewArraylist<treelinknode>(); + intLength =current.size (); - for(inti = 0; i < length; i++) { +Treelinknode Currenttreenode =Current.get (i); A if(I < Length-1) atCurrenttreenode.next = Current.get (i + 1); - Else -Currenttreenode.next =NULL; - if(Currenttreenode.left! =NULL) - Next.add (currenttreenode.left); - if(Currenttreenode.right! =NULL) in Next.add (currenttreenode.right); - } toCurrent =Next; + } - } the}Solution 2--Recursive
Consider for one node, the connection was done when:
1. Its left node (if exists) and connect to it right node.
2. Its right node (if exists) connect to its next node's left child.
3. Its left child and right child is all connected.
Note the prerequisite for this problem are perfect binary tree (every parent has both children). Time complexity O (n), Space cost O (1).
1 /**2 * Definition for binary tree with next pointer.3 * public class Treelinknode {4 * int val;5 * Treelinknode left, right, next;6 * Treelinknode (int x) {val = x;}7 * }8 */9 Public classSolution {Ten Public voidConnect (Treelinknode root) { One if(Root = =NULL) A return; - if(Root.left! =NULL) -Root.left.next =Root.right; the if(Root.right! =NULL&& Root.next! =NULL) -Root.right.next =Root.next.left; - Connect (root.left); - Connect (root.right); + } -}Solution 3--Four pointers
We can use four pointers to traverse the tree.
(Referrence:programcreek)
1 /**2 * Definition for binary tree with next pointer.3 * public class Treelinknode {4 * int val;5 * Treelinknode left, right, next;6 * Treelinknode (int x) {val = x;}7 * }8 */9 Public classSolution {Ten Public voidConnect (Treelinknode root) { One if(Root = =NULL) A return; -Treelinknode lasthead = root;//prevous level ' s head -Treelinknode lastcurrent =NULL;//previous level ' s pointer theTreelinknode Currenthead =NULL;//Current level ' s head -Treelinknode current =NULL;//Current level ' s pointer - - while(Lasthead! =NULL) { +Lastcurrent =Lasthead; -Currenthead =Lasthead.left; +Current =Lastcurrent.left; A while(Lastcurrent! =NULL&& Current! =NULL) { atCurrent.next =Lastcurrent.right; -Current =Current.next; -Lastcurrent =Lastcurrent.next; - if(Lastcurrent! =NULL) { -Current.next =Lastcurrent.left; -Current =Current.next; in } - } toLasthead =Currenthead; +Currenthead =NULL; - } the } *}
Populating Next right pointers in each Node solution