/**
* 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) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (!root) return;
if (!root->left || !root->right)
return;
TreeLinkNode* next = NULL;
TreeLinkNode* p = root;
while (p)
{
if (p->next)
next = p->next->left;
else
next = NULL;
p->left->next = p->right;
p->right->next = next;
p = p->next;
}
connect(root->left);
}
};[Leetcode] Populating Next Right Pointers in Each Node
最新推荐文章于 2021-09-24 12:05:40 发布
本文介绍了一种用于填充二叉树中每个节点的NEXT指针的算法,使得每一个节点的NEXT指针指向其下一个顺序遍历的节点。通过递归的方式实现了二叉树节点的连接,详细解释了算法的工作原理。
2921

被折叠的 条评论
为什么被折叠?



