您的位置:首页 > Web前端 > Node.js

LeetCode--Populating Next Right Pointers in Each Node

2014-08-22 21:12 411 查看
由于题目意思是满二叉树:

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

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

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

/**
* Definition for binary tree with next pointer.
* struct TreeLinkNode {
*  int val;
*  TreeLinkNode *left, *right, *next;
*  TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
* };
*/
class Solution {
public:
void connect(TreeLinkNode *root) {
if(root == NULL)
return;
if(root->left != NULL){
root->left->next = root->right;
}
if(root->right != NULL){
root->right->next = root->next?root->next->left:NULL;
}
connect(root->left);
connect(root->right);
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: