-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflatten.go
32 lines (29 loc) · 997 Bytes
/
flatten.go
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
package problem0114
import . "leetcodedaily/helpers/binarytree"
/*
Given the root of a binary tree, flatten the tree into a "linked list":
The "linked list" should use the same TreeNode class where the right child pointer points to the next node in the list and the left child pointer is always null.
The "linked list" should be in the same order as a pre-order traversal of the binary tree.
*/
func flatten(root *TreeNode) {
var prev *TreeNode
for root != nil {
// If there's no left node, we dont' need to move anything
if root.Left == nil {
root = root.Right
continue
}
// Traversing to the end of the left tree, where the right tree should start
prev = root.Left
for prev.Right != nil {
prev = prev.Right
}
// Putting the entire right side of the tree and the end of the left tree
prev.Right = root.Right
// Moving left side to right side, and nilling the left node
root.Right = root.Left
root.Left = nil
// Moving to next node
root = root.Right
}
}