-
Notifications
You must be signed in to change notification settings - Fork 0
/
114.py
53 lines (51 loc) · 1.86 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
# 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:
"""
解題思路:
第一個想法是把right節點儲存在stack內, 然後依序將左節點去替換掉右邊的節點, 最後再依序把右邊節點接下去,
但實作上遇到問題, 因此先採最保守的解法(解法1)
1. 走訪完所有節點, 再依序替換回去node裏面
2. 和linked list一樣, 基本上遇到in-place的問題就優先考慮做dummy節點, 結合一開始的思路, 節點儲存在stack FIFO, 依序走訪即可, 參考下解:
https://leetcode.com/problems/flatten-binary-tree-to-linked-list/discuss/37065/simple-dfs-python-solution
"""
# def dfs(self, root, values):
# if not root:
# return
# values.append(root.val)
# self.dfs(root=root.left, values=values)
# self.dfs(root=root.right, values=values)
#
# def flatten(self, root: Optional[TreeNode]) -> None:
# """
# Do not return anything, modify root in-place instead.
# """
# if not root:
# return
#
# values = list()
# self.dfs(root=root, values=values)
#
# for value in values[1:]:
# root.left = None
# root.right = TreeNode(value)
# root = root.right
def flatten(self, root: Optional[TreeNode]) -> None:
"""
Do not return anything, modify root in-place instead.
"""
dummy = TreeNode()
stack = [root]
while stack:
node = stack.pop()
if not node:
continue
dummy.right = node
dummy.left = None
stack.append(node.right)
stack.append(node.left)
dummy = node