-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path116.py
More file actions
40 lines (38 loc) · 1.09 KB
/
116.py
File metadata and controls
40 lines (38 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# Definition for binary tree with next pointer.
# class TreeLinkNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
# self.next = None
class Solution:
# @param root, a tree link node
# @return nothing
def connect(self, root):
head = tail = TreeLinkNode(0)
while root:
for c in (root.left, root.right):
tail.next = c
if c:
tail = tail.next
else:
break
if root.next:
root = root.next
else:
root = head.next
tail = head
# 另外一种方案,利用了满二叉树的性质
def connect(self, root):
if not root: return
cur = root
nex = root.left
while cur.left:
print(cur.val)
cur.left.next = cur.right
if cur.next:
cur.right.next = cur.next.left
cur = cur.next
else:
cur = nex
nex = cur.left