Skip to content

Commit 40e5d52

Browse files
committed
Sync LeetCode submission Runtime - 39 ms (66.71%), Memory - 18.5 MB (17.56%)
1 parent c85adf0 commit 40e5d52

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<p>Convert a <strong>Binary Search Tree</strong> to a sorted <strong>Circular Doubly-Linked List</strong> in place.</p>
2+
3+
<p>You can think of the left and right pointers as synonymous to the predecessor and successor pointers in a doubly-linked list. For a circular doubly linked list, the predecessor of the first element is the last element, and the successor of the last element is the first element.</p>
4+
5+
<p>We want to do the transformation <strong>in place</strong>. After the transformation, the left pointer of the tree node should point to its predecessor, and the right pointer should point to its successor. You should return the pointer to the smallest element of the linked list.</p>
6+
7+
<p>&nbsp;</p>
8+
<p><strong class="example">Example 1:</strong></p>
9+
10+
<p><img src="https://assets.leetcode.com/uploads/2018/10/12/bstdlloriginalbst.png" style="width: 100%; max-width: 300px;" /></p>
11+
12+
<pre>
13+
<strong>Input:</strong> root = [4,2,5,1,3]
14+
15+
<img src="https://assets.leetcode.com/uploads/2018/10/12/bstdllreturndll.png" style="width: 100%; max-width: 450px;" />
16+
<strong>Output:</strong> [1,2,3,4,5]
17+
18+
<strong>Explanation:</strong> The figure below shows the transformed BST. The solid line indicates the successor relationship, while the dashed line means the predecessor relationship.
19+
<img src="https://assets.leetcode.com/uploads/2018/10/12/bstdllreturnbst.png" style="width: 100%; max-width: 450px;" />
20+
</pre>
21+
22+
<p><strong class="example">Example 2:</strong></p>
23+
24+
<pre>
25+
<strong>Input:</strong> root = [2,1,3]
26+
<strong>Output:</strong> [1,2,3]
27+
</pre>
28+
29+
<p>&nbsp;</p>
30+
<p><strong>Constraints:</strong></p>
31+
32+
<ul>
33+
<li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li>
34+
<li><code>-1000 &lt;= Node.val &lt;= 1000</code></li>
35+
<li>All the values of the tree are <strong>unique</strong>.</li>
36+
</ul>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Approach 1: Recursion
2+
3+
# Time: O(n)
4+
# Space: O(n)
5+
6+
"""
7+
# Definition for a Node.
8+
class Node:
9+
def __init__(self, val, left=None, right=None):
10+
self.val = val
11+
self.left = left
12+
self.right = right
13+
"""
14+
15+
class Solution:
16+
def treeToDoublyList(self, root: 'Optional[Node]') -> 'Optional[Node]':
17+
def helper(node):
18+
# Inorder traversal
19+
nonlocal first, last
20+
if node:
21+
helper(node.left)
22+
23+
if last:
24+
# Link the previous node (last) with the current one (node)
25+
last.right = node
26+
node.left = last
27+
else:
28+
# Keep the smallest node to close Doubly Linked List later on
29+
first = node
30+
last = node
31+
32+
helper(node.right)
33+
34+
if not root:
35+
return None
36+
37+
first, last = None, None
38+
helper(root)
39+
40+
# Close DLL
41+
last.right = first
42+
first.left = last
43+
44+
return first
45+
46+

0 commit comments

Comments
 (0)