首页 > 其他 > 详细

LeetCode--Populating Next Right Pointers in Each Node

时间:2014-08-22 22:24:49      阅读:353      评论:0      收藏:0      [点我收藏+]

由于题目意思是满二叉树:

所以,对当前节点,设置它的左右子节点的next指针即可

root->left->next = root->right

root->right->next = root->next?root->next->left:NULL

 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 == NULL)
13             return;
14         if(root->left != NULL){
15             root->left->next = root->right;
16         }
17         if(root->right != NULL){
18             root->right->next = root->next?root->next->left:NULL;
19         }
20         connect(root->left);
21         connect(root->right);
22     }
23 };

 

LeetCode--Populating Next Right Pointers in Each Node

原文:http://www.cnblogs.com/cane/p/3930258.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!