-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path114_flatten-binary-tree-to-linked-list.py
61 lines (56 loc) · 1.73 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#@author: rye
#@time: 2019/4/1
# Definition for a binary tree node.
class TreeNode(object):
def __init__(self, x):
self.val = x
self.left = None
self.right = None
'''
解法:
1. copy the left and right subtree
2. then cut root’s left subtree
3. do DFS
4. left and right has been flattened and connect them left and right back to the root
'''
class Solution(object):
def flatten(self, root):
"""
:type root: TreeNode
:rtype: void Do not return anything, modify root in-place instead.
"""
if not root:
return
left_node = root.left
right_node = root.right
root.left = None
self.flatten(left_node)
self.flatten(right_node)
if left_node:
root.right = left_node # 相对于链表插入的第一步
while left_node.right:
left_node = left_node.right
left_node.right = right_node # 相对于链表插入的第二步
# 另一个大佬的代码
class Solution1(object):
def flatten(self, root):
"""
:type root: TreeNode
:rtype: void Do not return anything, modify root in-place instead.
"""
if not root:
return root
self.flatten(root.left)
self.flatten(root.right)
while root.left is not None:
right = root.right
root.right = root.left # 插入第一步
root.left = None
node = root.right # 插入第二步
while node.right is not None:
node = node.right
node.right = right
if __name__ == '__main__':
pass