-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrecover-binary-search-tree.js
98 lines (88 loc) · 2.16 KB
/
recover-binary-search-tree.js
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
/**
* Source: https://leetcode.com/problems/recover-binary-search-tree/
* Tags: [Tree,Depth-first Search]
* Level: Hard
* Title: Recover Binary Search Tree
* Auther: @imcoddy
* Content: Two elements of a binary search tree (BST) are swapped by mistake.
*
* Recover the tree without changing its structure.
*
*
* Note:
* A solution using O(n) space is pretty straight forward. Could you devise a constant space solution?
*
*
* confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.
*
* OJ's Binary Tree Serialization:
*
* The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.
*
*
* Here's an example:
*
* 1
* / \
* 2 3
* /
* 4
* \
* 5
*
* The above binary tree is serialized as "{1,2,3,#,#,4,#,#,5}".
*/
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {void} Do not return anything, modify root in-place instead.
*/
/**
* Memo: use inorder traverse to put each nodes in a row, find out the wrongly swapped pair, and swap it back
* Complex: O(n)
* Runtime: 324ms
* Tests: 1916 test cases passed
* Rank: C
*/
var recoverTree = function(root) {
var nodes = [];
function traverse(root) {
if (!root) return;
traverse(root.left);
nodes.push(root);
traverse(root.right);
}
traverse(root);
if (nodes.length < 2) return;
var head = 0;
var tail = nodes.length - 1;
while (head < tail && nodes[head].val < nodes[head + 1].val) {
head++;
}
while (tail > head && nodes[tail].val > nodes[tail - 1].val) {
tail--;
}
var tmp = nodes[head].val;
nodes[head].val = nodes[tail].val;
nodes[tail].val = tmp;
};
function TreeNode(val) {
this.val = val;
this.left = this.right = null;
}
var node = new TreeNode(4);
var root = node;
node = new TreeNode(3);
root.right = node;
node = new TreeNode(1);
root.left = node;
node = new TreeNode(2);
root.right.right = node;
recoverTree(root);
console.log(root);