-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathFlattenBinaryTreeToLinkedList_v0.py
More file actions
65 lines (51 loc) · 1.27 KB
/
FlattenBinaryTreeToLinkedList_v0.py
File metadata and controls
65 lines (51 loc) · 1.27 KB
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
#!/usr/bin/env python
# encoding: utf-8
# Definition for a binary tree node.
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
# @param {TreeNode} root
# @return {void} Do not return anything, modify root in-place instead.
def flatten(self, root):
if not root:
return
r = []
self.preorder(root, r)
for i in range(len(r)-1):
r[i].right = r[i+1]
r[i].left = None
r[len(r)-1].right = None
r[len(r)-1].left = None
def preorder(self, root, result):
if not root:
return
result.append(root)
self.preorder(root.left, result)
self.preorder(root.right, result)
def inorder(self, root):
if not root:
return
self.inorder(root.left)
print root.val
self.inorder(root.right)
if __name__ == '__main__':
s = Solution()
n1 = TreeNode(1)
n2 = TreeNode(2)
n3 = TreeNode(3)
n4 = TreeNode(4)
n5 = TreeNode(5)
n6 = TreeNode(6)
root = n1
n1.left = n2
n2.left = n3
n2.right = n4
n1.right = n5
n5.right = n6
s.inorder(root)
print '-' * 6
s.flatten(root)
s.inorder(root)