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

 

注意是完美二叉树,所以

(1)根据题述:左孩子为空,则右孩子一定为空,所以左孩子为空,则return
(2)如果左孩子不为空,则右孩子一定不为空,所以链接左孩子和右孩子即可(左孩子的next赋值为右孩子)

 

C++:

 1 class Solution {
 2 public:
 3     void connect(TreeLinkNode *root) {
 4         
 5         if(root==NULL) return ;
 6         
 7         if(root->left&&root->right)
 8         {
 9             root->left->next=root->right;
10             if(root->next)
11                 root->right->next=root->next->left;
12             else
13                 root->right->next=NULL;
14         }
15             
16         connect(root->left);
17         connect(root->right);
18             
19         
20     }
21 };

 

Python:

 1 # Definition for binary tree with next pointer.
 2 # class TreeLinkNode:
 3 #     def __init__(self, x):
 4 #         self.val = x
 5 #         self.left = None
 6 #         self.right = None
 7 #         self.next = None
 8 
 9 class Solution:
10     # @param root, a tree link node
11     # @return nothing
12     def connect(self, root):
13         if root is None:return
14         if root.left and root.right:
15             root.left.next=root.right
16             if root.next:
17                 root.right.next=root.next.left
18             else:
19                 root.right.next=None
20         self.connect(root.left)
21         self.connect(root.right)

 

 

II:

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

 1 class Solution {
 2 public:
 3     void connect(TreeLinkNode *root) {
 4         if(root==NULL) return ;
 5 
 6         TreeLinkNode * p=root;
 7         TreeLinkNode * q=NULL;
 8         TreeLinkNode * nextNode=NULL;
 9 
10         while(p)
11         {
12             if(p->left)
13             {
14                 if(q)
15                     q->next=p->left;
16                 q=p->left;
17                 if(nextNode==NULL)
18                     nextNode=q;
19             }
20 
21             if(p->right)
22             {
23                 if(q)
24                     q->next=p->right;
25                 q=p->right;
26                 if(nextNode==NULL)
27                     nextNode=q;
28             }
29 
30             p=p->next;
31         }
32 
33            connect(nextNode);
34     }
35 };

 

posted on 2015-04-10 00:30  黄瓜小肥皂  阅读(160)  评论(0编辑  收藏  举报