-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path114.py
29 lines (27 loc) · 829 Bytes
/
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
# 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: TreeNode) -> None:
"""
Do not return anything, modify root in-place instead.
"""
if not root:
return
all_nodes = []
nodes = [root]
while nodes:
node = nodes.pop()
if not node:
continue
all_nodes.append(node)
nodes.append(node.right)
nodes.append(node.left)
for i in range(len(all_nodes) - 1):
all_nodes[i].right = all_nodes[i + 1]
all_nodes[i].left = None
all_nodes[-1].right = None
all_nodes[-1].left = None