Leetcode OJ: Populating Next Right Pointers in Each Node I/II 非递归、常量空间
Populating Next Right Pointers in Each Node
Given a binary tree
struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }
Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to
NULL
.Initially, all next pointers are set to
NULL
.Note:
- You may only use constant extra space.
- You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children).
For example,
Given the following perfect binary tree,1 / \ 2 3 / \ / \ 4 5 6 7
After calling your function, the tree should look like:
1 -> NULL / \ 2 -> 3 -> NULL / \ / \ 4->5->6->7 -> NULL
题目要求要常量空间,那么层次遍历是不能用的了,在网上看到的代码基本都是递归实现,事实上递归实现是需要栈空间的。
这题的关键是要利用好next指针以及完美二叉树的特点。
当父节点的next都连接好了之后,子节点是可以通过父节点的next去访问旁边子树的节点的。
而完美二叉树又保证了下一层的第一个节点是必然从最左侧开始的,所以保存当前层的节点开头,就可以在处理完当前层后,返回到开始就可以进行下一层的连接了。
代码如下:
1 /** 2 * Definition for binary tree with next pointer. 3 * struct TreeLinkNode { 4 * int val; 5 * TreeLinkNode *left, *right, *next; 6 * TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 void connect(TreeLinkNode *root) { 12 if (NULL == root) 13 return; 14 root->next = NULL; 15 TreeLinkNode* parent = root; 16 TreeLinkNode* child = root->left; 17 while (child) { 18 TreeLinkNode* tmp = child; //保存开始点 19 while (child) { 20 child->next = parent->right; 21 child = child->next; 22 parent = parent->next; 23 if (child) { // 若child有右兄弟 24 // 看parent的兄弟是否有右兄弟 25 child->next = parent ? parent->left : NULL; 26 child = child->next; 27 } 28 } 29 // 处理下一层 30 parent = tmp; 31 child = tmp->left; 32 } 33 } 34 };
好不容易把这题给弄明白了,然后Leetcode上又出现了这题的变种,不是完美二叉树又怎样呢?
Follow up for problem "Populating Next Right Pointers in Each Node".
What if the given tree could be any binary tree? Would your previous solution still work?
Note:
- You may only use constant extra space.
For example,
Given the following binary tree,1 / \ 2 3 / \ \ 4 5 7
After calling your function, the tree should look like:
1 -> NULL / \ 2 -> 3 -> NULL / \ \ 4-> 5 -> 7 -> NULL
这是针对任意二叉树的,区别在哪里?
上面提到过,完美二叉树可以保证下一层的开始在最左侧。另外,在判断当前层是否结束除了遇到parent为空外,还可以是parent左或右孩子为空,这样可以减少计算。
因此,这里面就是要解决两个问题:
1. 怎么找到下一层的开始?
2. 开始后怎么连起来?
3. 怎么判断当前层已经结束?
第1个问题可以用变量去存第一个有孩子的点。这里我借用了一个虚拟的头节点。
第2个问题则需要一个变量存上一次连接上的点。
第3个问题则只能是遍历当前层。
代码如下:
1 /** 2 * Definition for binary tree with next pointer. 3 * struct TreeLinkNode { 4 * int val; 5 * TreeLinkNode *left, *right, *next; 6 * TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 void connect(TreeLinkNode *root) { 12 if (NULL == root) 13 return; 14 root->next = NULL; 15 TreeLinkNode* parent = root; 16 TreeLinkNode vhead(0); 17 while (parent) { 18 TreeLinkNode* pre = &vhead; 19 while (parent) { 20 if (parent->left) { 21 pre->next = parent->left; 22 pre = pre->next; 23 } 24 if (parent->right) { 25 pre->next = parent->right; 26 pre = pre->next; 27 } 28 parent = parent->next; 29 } 30 if (vhead.next) 31 parent = vhead.next; 32 vhead.next = NULL; 33 } 34 } 35 };
这么一看,这通用代码貌似要比完美二叉树的代码漂亮多了~