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
https://leetcode.com/problems/populating-next-right-pointers-in-each-node/
Because of the space complexity of O (1), wide search can not be used, can only consider recursive iteration. Taking advantage of the next pointer of parent, we can easily find the next pointer to the child node.
//C + + RECURSIVE CODE:
class solution { /span>public : static void Connect (treelinknode* if (root = NULL) return ; if (root->left) Root->left->next = Root->right; if (root->next && root->right) Root->right->next = Root->next->left; Connect (Root ->left); Connect (Root ->right); }};
In fact, using stacks also consumes space, and iterations should be the most appropriate approach. To maintain the consistency of the processing, we can add a dummy head node to each layer and then decide on the child layer's next based on the parent layer (using next).
//JAVA iterative CODE:
Public classSolution { Public voidConnect (Treelinknode root) {Treelinknode dummy=NewTreelinknode (0); while(Root! =NULL) {Treelinknode child=dummy; Dummy.next=NULL; while(Root! =NULL){ if(Root.left! =NULL) {Child.next=Root.left; Child=Child.next; } if(Root.right! =NULL) {Child.next=Root.right; Child=Child.next; } Root=Root.next; } Root=Dummy.next; } }}
PYTHON iterative CODE:
classSolution:#@param root, a tree node #@return Nothing defConnect (self, root): Dummychild=treelinknode (0) whileRoot:cur=Dummychild Dummychild.next=None whileRoot:ifRoot.left:cur.next=root.left cur=Cur.nextifRoot.right:cur.next=root.right cur=Cur.next Root=Root.next Root= Dummychild.next
View Code
Follow up to Problem "populating Next right pointersin each Node".
What if the given tree could is any binary tree? Would your previous solution still work?
Note:
For example,
Given The following binary tree,
1 / 2 3 /\ 4 5 7
After calling your function, the tree is should look like:
1, NULL / 2, 3, null /\ 4-> 5, 7, NULL
https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/
The only difference is that the next pointer to the child node is not so straightforward, you can use the function to return according to different circumstances, the other part of the same procedure.
classSolution { Public: Treelinknode* First (treelinknode*root) {Root= root->Next; while(root) {if(Root->left)returnRoot->Left ; Else if(Root->right)returnRoot->Right ; Root= root->Next; } returnNULL; } voidConnect (treelinknode*cur) {Treelinknode* Root =cur; if(Root = NULL)return; while(root) {if(root->Left ) {Root->left->next = Root->right? Root->Right:first (root); } if(root->Right ) {Root->right->next =First (root); } Root= root->Next; } Connect (cur-Left ); Connect (cur-Right ); }};
Populating Next right pointers in each Node Ii--leetcode puzzle series