-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path114.py
More file actions
57 lines (52 loc) · 1.83 KB
/
114.py
File metadata and controls
57 lines (52 loc) · 1.83 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# 這題是要把一個樹變成只有右邊一條的 preordered link list(意思就是左節點是None)
# 做法就是 preorder 走一遍之後再逐個點做重新連結
# 還有個做法就是把每個點的右子樹都拉到該節點的最右下角,然後再移動左子樹到右邊去,最後把左邊掛上 None
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def flatten(self, root: Optional[TreeNode]) -> None:
"""
Do not return anything, modify root in-place instead.
"""
stack = []
ls = []
cur = root
while stack or cur:
while cur:
stack.append(cur)
ls.append(cur)
cur = cur.left
cur = stack.pop()
cur = cur.right
for i in range(len(ls)):
if i == len(ls)-1:
ls[i].left = None
ls[i].right = None
else:
ls[i].left = None
ls[i].right = ls[i+1]
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def flatten(self, root: Optional[TreeNode]) -> None:
"""
Do not return anything, modify root in-place instead.
"""
cur = root
while cur:
if cur.left:
prev = cur.left
while prev.right:
prev = prev.right
prev.right = cur.right
cur.right = cur.left
cur.left = None
cur = cur.right