-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTree.py
More file actions
44 lines (36 loc) · 1.17 KB
/
Copy pathTree.py
File metadata and controls
44 lines (36 loc) · 1.17 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
######################################################################################
# leetcode 426 Convert Binary Search Tree to Sorted Doubly Linked List
######################################################################################
"""
Definition of TreeNode:
class TreeNode:
def __init__(self, val):
self.val = val
self.left, self.right = None, None
"""
class Solution:
"""
@param root: root of a tree
@return: head node of a doubly linked list
"""
def treeToDoublyList(self, root):
# Write your code here.
def inorder_dfs(node):
if not node:
return None, None
if not node.left and not node.right:
return node, node
left, right = node, node
if node.right:
s_r, right = inorder_dfs(node.right)
node.right = s_r
s_r.left = node
if node.left:
left, e_l = inorder_dfs(node.left)
e_l.right = node
node.left = e_l
return left, right
s, e = inorder_dfs(root)
s.left = e
e.right = s
return s