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
Subscribe to see which companies asked this question
Binary tree for hierarchical traversal//judging the bottom-level condition: If the currently accessed layer has a child node in either node, the current layer is not the lowest level. Class solution{public:treelinknode* Nextleft;bool hastotheend;void Connect (treelinknode *root) {if (!root) return ; hastotheend = False;int level = 1;while (!hastotheend) {hastotheend= true;nextleft= NULL; Visitlevel (root, level); ++level;}} void Visitlevel (treelinknode* node, int level) {if (level = = 1) {if (nextleft! = NULL) {nextleft->next = node;} Nextleft = Node;if (node->left! = NULL | | Node->right! = NULL) {hastotheend= false;}} if (node->left) visitlevel (Node->left, LEVEL-1), if (node->right) visitlevel (Node->right, Level -1);};
Leetcode 117:populating Next Right pointers in each Node II