-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
112 lines (98 loc) · 2.95 KB
/
main.go
File metadata and controls
112 lines (98 loc) · 2.95 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Source: https://leetcode.com/problems/convert-binary-search-tree-to-sorted-doubly-linked-list
// Title: Convert Binary Search Tree to Sorted Doubly Linked List
// Difficulty: Medium
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Convert a **Binary Search Tree** to a sorted **Circular Doubly-Linked List** in place.
//
// 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.
//
// We want to do the transformation **in place**. 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.
//
// **Example 1:**
//
// https://assets.leetcode.com/uploads/2018/10/12/bstdlloriginalbst.png
//
// ```
// Input: root = [4,2,5,1,3]
//
// https://assets.leetcode.com/uploads/2018/10/12/bstdllreturndll.png
// Output: [1,2,3,4,5]
//
// Explanation: The figure below shows the transformed BST. The solid line indicates the successor relationship, while the dashed line means the predecessor relationship.
// https://assets.leetcode.com/uploads/2018/10/12/bstdllreturnbst.png
// ```
//
// **Example 2:**
//
// ```
// Input: root = [2,1,3]
// Output: [1,2,3]
// ```
//
// **Constraints:**
//
// - The number of nodes in the tree is in the range `[0, 2000]`.
// - `-1000 <= Node.val <= 1000`
// - All the values of the tree are **unique**.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package main
type Node struct {
Val int
Left *Node
Right *Node
}
// Use DFS
func treeToDoublyList(root *Node) *Node {
if root == nil {
return nil
}
// DFS
var dfs func(node *Node) (*Node, *Node) // return left/right most node
dfs = func(node *Node) (*Node, *Node) {
left, right := node, node
if node.Left != nil {
leftLeft, leftRight := dfs(node.Left)
leftRight.Right = node
node.Left = leftRight
left = leftLeft
}
if node.Right != nil {
rightLeft, rightRight := dfs(node.Right)
rightLeft.Left = node
node.Right = rightLeft
right = rightRight
}
return left, right
}
left, right := dfs(root)
left.Left = right
right.Right = left
return left
}
// Use DFS + Extra Space
func treeToDoublyList2(root *Node) *Node {
if root == nil {
return nil
}
var nodes []*Node
// DFS
var dfs func(node *Node)
dfs = func(node *Node) {
if node == nil {
return
}
dfs(node.Left)
nodes = append(nodes, node)
dfs(node.Right)
}
dfs(root)
// Build linked list
n := len(nodes)
for i := range n {
nodes[i].Right = nodes[(i+1)%n]
nodes[(i+1)%n].Left = nodes[i]
}
return nodes[0]
}