Skip to content

Commit f8c532a

Browse files
authored
Create Flatten-Binary-Tree-to-Linked-List.py
1 parent eaccd1b commit f8c532a

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution:
9+
def flatten(self, root: TreeNode) -> None:
10+
"""
11+
Do not return anything, modify root in-place instead.
12+
"""
13+
if root == None:
14+
return None
15+
16+
self.dfs_list = []
17+
18+
def dfs(root):
19+
if root:
20+
self.dfs_list.append(root)
21+
dfs(root.left)
22+
dfs(root.right)
23+
24+
dfs(root)
25+
root = self.dfs_list.pop(0)
26+
while self.dfs_list:
27+
node = self.dfs_list.pop(0)
28+
root.right = node
29+
root.left = None
30+
root = root.right
31+
return root

0 commit comments

Comments
 (0)