LeetCode: Populating Next Right Pointer in Each Node

LeetCode: Populating Next Right Pointer 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

地址:https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node/

算法:用递归好简单有木有。按上面的例子,先完成左右子树的连接,然后,根的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(!root)   return ;
13         root->next = NULL;
14         connect(root->left);
15         connect(root->right);
16         TreeLinkNode *p = root->left;
17         TreeLinkNode *q = root->right;
18         while(p && q){
19             p->next = q;
20             p = p->right;
21             q = q->left;
22         }
23     }
24 };

第二题:

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

地址:https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/

算法:这道题与前面一题不同的是,这里的二叉树不再是完美二叉树,而是一颗普通的二叉树。由于是一颗普通的二叉树,所以沿着右指针走不一定会到达下一层的最后一个节点,因为这个右指针可能为空;同样,沿着左指针走也不一定会到达下一层的第一个节点,因为这个左指针可能为空。所以,我们需要一个找到当前节点下一层的第一个节点的函数,然后顺着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(!root)   return ;
13         root->next = NULL;
14         connect(root->left);
15         connect(root->right);
16         TreeLinkNode *p = root->left;
17         TreeLinkNode *q = root->right;
18         while(p && q){
19             TreeLinkNode *r = nextLevelFirst(p);
20             while(p->next){
21                 p = p->next;
22             }
23             p->next = q;
24             p = r;
25             q = nextLevelFirst(q);
26         }
27     }
28     TreeLinkNode * nextLevelFirst(TreeLinkNode *p){
29         while(p){
30             if(p->left){
31                 return p->left;
32             }else if(p->right){
33                 return p->right;
34             }
35             p = p->next;
36         }
37         return NULL;
38     }
39 };
原文地址:https://www.cnblogs.com/boostable/p/leetcode_populating_next_right_pointer_in_each_node.html