/**
* 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 )
{
return ;
}
root->left->next = root->right;
root->right->next = root->next ? root->next->left : NULL;
connect( root->left );
connect( root->right );
}
};Populating Next Right Pointers in Each Node
最新推荐文章于 2022-02-28 18:16:11 发布
本文介绍了一种用于构建带有next指针的二叉树节点的算法,详细解释了如何通过递归方式连接左右子节点。
734

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



