-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path114.py
57 lines (51 loc) · 1.63 KB
/
114.py
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
__________________________________________________________________________________________________
sample 28 ms submission
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def flatten(self, root: TreeNode) -> None:
"""
Do not return anything, modify root in-place instead.
"""
st=[root];head1=TreeNode(0)
if not root:
return
head=head1
while st:
x=st.pop()
head.right=x
if x.right:
st.append(x.right)
if x.left:
st.append(x.left)
x.left=None
head=head.right
head.right=None
__________________________________________________________________________________________________
sample 13220 kb submission
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
def get_nodes(root):
if not root:
return []
left = get_nodes(root.left)
right = get_nodes(root.right)
return [root] + left + right
class Solution:
def flatten(self, root: TreeNode) -> None:
"""
Do not return anything, modify root in-place instead.
"""
ordered = get_nodes(root)
for i, node in enumerate(ordered[:-1]):
node.left = None
node.right = ordered[i + 1]
__________________________________________________________________________________________________