Populating Next Right Pointers in Each Node II--leetcode难题讲解系列

本文介绍了一种在完美二叉树及一般二叉树中填充每个节点的next指针指向其右侧相邻节点的方法。提供了递归和迭代两种实现方式,并针对不同类型的二叉树进行了详细解析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Given a binary tree

    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

https://leetcode.com/problems/populating-next-right-pointers-in-each-node/

由于空间复杂度为O(1),广搜深搜不能使用,只能考虑递归迭代。充分利用parent的next指针,我们可以很容易的找到子节点的next指针。

// C++ RECURSIVE CODE: 
class
Solution { public: static void connect(TreeLinkNode* root){ if(root == NULL) return; if(root->left) root->left->next = root->right; if(root->next && root->right ) root->right->next = root->next->left; connect(root->left); connect(root->right); } };

 实际上用栈也是会消耗空间的,迭代应该是最合适的办法。为了保持处理的一致性,我们可以给每一层加一个dummy头结点,然后根据parent层(利用next)决定child层的next。

// JAVA ITERATIVE CODE:
public
class Solution { public void connect(TreeLinkNode root) { TreeLinkNode dummy = new TreeLinkNode(0); while(root != null){ TreeLinkNode child = dummy; dummy.next = null; while(root != null){ if(root.left != null){ child.next = root.left; child = child.next; } if(root.right != null){ child.next = root.right; child = child.next; } root = root.next; } root = dummy.next; } } }
PYTHON ITERATIVE CODE:
class Solution:
    # @param root, a tree node
    # @return nothing
    def connect(self, root):
        dummychild = TreeLinkNode(0)
        while root:
            cur = dummychild
            dummychild.next = None
            while root:
                if root.left:
                    cur.next = root.left
                    cur = cur.next
                if root.right:
                    cur.next = root.right
                    cur = cur.next
                root = root.next
            root = dummychild.next
View Code
 
  
 

 

 

 

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
https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/


唯一的区别是找子节点的next指针不是那么直接了,可以用函数根据不同情况来返回,其他的部分做法一样。
class Solution {
public:
    TreeLinkNode* first(TreeLinkNode* root){
        root = root->next;
        while(root){
            if(root->left) return root->left;
            else if(root->right) return root->right;
            root = root->next;
        }
        return NULL;
    }
    void connect(TreeLinkNode* cur){
        TreeLinkNode* root = cur;
        if(root == NULL) return;
        while(root){
            if(root->left){
                root->left->next = root->right ? root->right: first(root);
            }
            if(root->right){
                root->right->next = first(root);
            }
            root = root->next;
        }
        connect(cur->left);
        connect(cur->right);
    }
};
 
 

 

 

转载于:https://www.cnblogs.com/huashiyiqike/p/4897263.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值