-
Notifications
You must be signed in to change notification settings - Fork 19
/
21_Tree2Doubly-Linked-List.py
60 lines (50 loc) · 1.35 KB
/
21_Tree2Doubly-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
#!/usr/bin/python
# coding=utf-8
'''
__author__ = 'sunp'
__date__ = '2019/2/13'
Given a tree, write a function to convert it into a circular doubly linked list from left to right by only modifying the existing pointers.
'''
class TreeNode(object):
def __init__(self, val, left=None, right=None):
self.val = val
self.left = left
self.right = right
class DllNode(object):
def __init__(self, val, prev=None, next=None):
self.val = val
self.prev = prev
self.next = next
def convert(root):
# InOrder + DllNode
head, pre = None, None
stack = []
while root or stack:
if root:
stack.append(root)
root = root.left
else:
node = stack.pop()
cur = DllNode(node.val)
cur.prev = pre
if pre:
pre.next = cur
pre = cur
if not head:
head = cur
root = node.right
return head
if __name__ == '__main__':
test = TreeNode(4)
test.left = TreeNode(2)
test.right = TreeNode(6)
test.left.left = TreeNode(1)
test.left.right = TreeNode(3)
test.right.left = TreeNode(5)
test.right.right = TreeNode(7)
res = convert(test)
outs = []
while res:
outs.append(res.val)
res = res.next
assert outs == [1, 2, 3, 4, 5, 6, 7]