1. Post - Order
class Solution:
def flatten(self, root: TreeNode) -> None:
stack, temp = [], []
cur = root
while True:
while cur:
temp.append(cur)
stack.append(cur)
cur = cur.left
if not stack:
break
cur = stack.pop()
cur = cur.right
for i in range(1, len(temp)):
temp[i - 1].left = None
temp[i - 1].right = temp[i]
return temp[0] if temp else []
Post - Order 순회로 접근이 가능하다.
'Algorithm' 카테고리의 다른 글
394. Decode String (0) | 2021.05.14 |
---|---|
621. Task Scheduler (0) | 2021.05.14 |
62. Unique Paths (0) | 2021.05.12 |
64. Minimum Path Sum (0) | 2021.05.12 |
199. Binary Tree Right Side View (0) | 2021.05.11 |