-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path426.py
More file actions
34 lines (28 loc) · 798 Bytes
/
Copy path426.py
File metadata and controls
34 lines (28 loc) · 798 Bytes
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
"""
# Definition for a Node.
class Node:
def __init__(self, val, left=None, right=None):
self.val = val
self.left = left
self.right = right
"""
class Solution:
def treeToDoublyList(self, root: 'Node') -> 'Node':
if not root:
return None
first,last=None,None
def dfs(root):
nonlocal last, first
if root:
dfs(root.left)
if last:
last.right=root
root.left=last
else:
first=root
last=root
dfs(root.right)
dfs(root)
first.left=last
last.right=first
return first