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