-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path114.flatten-binary-tree-to-linked-list.py
68 lines (59 loc) · 1.85 KB
/
114.flatten-binary-tree-to-linked-list.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
58
59
60
61
62
63
64
65
66
67
#
# @lc app=leetcode id=114 lang=python3
#
# [114] Flatten Binary Tree to Linked List
#
# @lc code=start
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
# TAGS: Tree, Depth First Search
class Solution:
"""
Do not return anything, modify root in-place instead.
There is a O(1) Space solution in the article (Morris Traversal)
"""
# 36 ms, 96%
def flatten(self, root: TreeNode) -> None:
# dfs to get all values
ans = []
stack = [root]
while stack:
node = stack.pop()
if node:
ans.append(node.val)
stack.extend([node.right, node.left])
# make a tree out of those values
for val in ans[1:]:
root.right = TreeNode(val)
root.left = None
root = root.right
def __init__(self):
self.prev = None
# Recursive from discussion. Time: O(N), Space: O(1)
def flatten(self, root: TreeNode) -> None:
if root:
self.flatten(root.right)
self.flatten(root.left)
root.right = self.prev
root.left = None
self.prev = root
# 32 ms, 90.55%. O(N) Time, O(N) Space because of stack
# Although we create a node here, we don't actually return it.
# We only use it as a head to handle edge case.
def flatten(self, root: TreeNode) -> None:
head = prev = TreeNode(0)
stack = [root]
while stack:
node = stack.pop()
if node:
stack.append(node.right)
stack.append(node.left)
if prev:
prev.right = node
node.left = None
prev = prev.right
# @lc code=end