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
/** * Definition for binary tree with next pointer. * struct Treelinknode {* int val; * Treelinknode *left, *right, *next; * treelinknode (int x): Val (x), left (N ULL), right (null), Next (null) {} *}; */class Solution {public: void Connect (Treelinknode *root) { if (root = NULL) return; queue<treelinknode*> use; Use.push (root); while (!use.empty ()) { int s = use.size (); for (int i=0; i<s; i++) { treelinknode* t = Use.front (); Use.pop (); if (i = = s-1) t->next = NULL; else T->next = Use.front (); if (t->left! = NULL) Use.push (t->left); if (t->right! = NULL) use.push (t->right);}}} ;
Leetcode--populating Next Right pointers with each Node II