leetcode 116 & 117. Populating Next Right Pointers in Each Node 的思路与python实现

You are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:

struct Node {
  int val;
  Node *left;
  Node *right;
  Node *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.

 

思路

这道题里的树是完全二叉树。

用一个head节点从根部一直往左走(left)。

每遍历到一个节点,用一个curr往右走,当前节点的左子节点Next到右子节点,当前节点的右子节点Next到Next节点的左子节点。

代码

循环写法,只用了两个节点,常数的空间复杂度,不过leetcode上显示不太好,不知道为什么。

class Solution:
    def connect(self, root: 'Node') -> 'Node':
        if not root:
            return None
        head = root
        while head.left:
            curr = head
            while curr:
                curr.left.next = curr.right
                if curr.next:
                    curr.right.next = curr.next.left
                curr = curr.next
            head = head.left
        return root

或者也可以递归写。每次只处理自己两个子节点的next,递归地完成。

class Solution:
    def connect(self, root: 'Node') -> 'Node':
        self.helper(root)
        return root
    
    def helper(self, root):
        if root and root.left:
            root.left.next = root.right
            if root.next:
                root.right.next = root.next.left
            self.helper(root.left)
            self.helper(root.right)

然后117就是把完全二叉树改成了普通二叉树。

思路是一样的,但是写法要改动。再每一层连接next的时候要一直存着目前遍历到最右的位置,然后找到更右的再连上。这里的代码有点像链表题的写法。要使用到dummy节点。


class Solution:
    def connect(self, root: 'Node') -> 'Node':
        if not root:
            return None
        head = root
        while head: # todo
            
            dummy = Node(None, None, None, None)
            curr = dummy
            head_move = head
            while head_move:
                if head_move.left:
                    curr.next = head_move.left
                    curr = curr.next
                if head_move.right:
                    curr.next = head_move.right
                    curr = curr.next
                head_move = head_move.next
            
            while head:
                if head.left:
                    head = head.left
                    break
                elif head.right:
                    head = head.right
                    break
                else:
                    head = head.next
        return root

也有简单的写法,就是直接层序遍历,但是理论上就不是常数空间复杂度了,因为要保存一整层的节点。从leetcode的表现来看,区别却不大....

class Solution:
    def connect(self, root: 'Node') -> 'Node':
        curr = root
        lastlist = [root]
        while len(lastlist) > 0:
            currlist = []
            for node in lastlist:
                if node:
                    if node.left:
                        currlist.append(node.left)
                    if node.right:
                        currlist.append(node.right)
            if len(currlist) > 1:
                for idx in range(len(currlist) - 1):
                    currlist[idx].next = currlist[idx + 1]
            lastlist = currlist
        return root

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值