Skip to content

Commit 7a5f8d0

Browse files
committed
udpate
1 parent 1cd4de9 commit 7a5f8d0

File tree

2 files changed

+91
-1
lines changed

2 files changed

+91
-1
lines changed

2024/meta/prep.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,3 +672,44 @@ def findMaxLength(self, nums: List[int]) -> int:
672672
else:
673673
res = max(res, j-seen[parity])
674674
return res
675+
676+
############# 426. Convert Binary Search Tree to Sorted Doubly Linked List #############
677+
class Solution:
678+
def treeToDoublyList(self, root: 'Optional[Node]') -> 'Optional[Node]':
679+
def dfs(node):
680+
"""
681+
Performs standard inorder traversal:
682+
left -> node -> right
683+
and links all nodes into DLL
684+
"""
685+
nonlocal last, first
686+
if node:
687+
# left
688+
dfs(node.left)
689+
690+
# node
691+
if last:
692+
# link the previous node (last)
693+
# with the current one (node)
694+
last.right = node
695+
node.left = last
696+
else:
697+
# keep the smallest node
698+
# to close DLL later on
699+
first = node
700+
last = node
701+
702+
# right
703+
dfs(node.right)
704+
705+
if not root:
706+
return None
707+
708+
# the smallest (first) and the largest (last) nodes
709+
first, last = None, None
710+
dfs(root)
711+
712+
# close DLL
713+
last.right = first
714+
first.left = last
715+
return first

binary-tree/traversal-preorder-inorder-postorder/426. Convert Binary Search Tree to Sorted Doubly Linked List.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,55 @@ def __init__(self, val, left=None, right=None):
66
self.left = left
77
self.right = right
88
"""
9+
"""
10+
# Definition for a Node.
11+
class Node:
12+
def __init__(self, val, left=None, right=None):
13+
self.val = val
14+
self.left = left
15+
self.right = right
16+
"""
17+
18+
class Solution:
19+
def treeToDoublyList(self, root: 'Optional[Node]') -> 'Optional[Node]':
20+
def dfs(node):
21+
"""
22+
Performs standard inorder traversal:
23+
left -> node -> right
24+
and links all nodes into DLL
25+
"""
26+
nonlocal last, first
27+
if node:
28+
# left
29+
dfs(node.left)
30+
31+
# node
32+
if last:
33+
# link the previous node (last)
34+
# with the current one (node)
35+
last.right = node
36+
node.left = last
37+
else:
38+
# keep the smallest node
39+
# to close DLL later on
40+
first = node
41+
last = node
42+
43+
# right
44+
dfs(node.right)
45+
46+
if not root:
47+
return None
48+
49+
# the smallest (first) and the largest (last) nodes
50+
first, last = None, None
51+
dfs(root)
52+
53+
# close DLL
54+
last.right = first
55+
first.left = last
56+
return first
57+
958
from collections import deque
1059
class Solution:
1160
def treeToDoublyList(self, root: 'Optional[Node]') -> 'Optional[Node]':
@@ -34,7 +83,7 @@ def treeToDoublyList(self, root: 'Optional[Node]') -> 'Optional[Node]':
3483
prev.right = curr
3584
curr.left = prev
3685
prev = curr
37-
86+
3887
# 最后一个节点:curr 和 stack 都没元素了
3988
if not stack and not curr.right:
4089
last = curr

0 commit comments

Comments
 (0)