Skip to content

Commit 316af28

Browse files
committed
Add js solution to leetcode problem: no.285
1 parent 30bb486 commit 316af28

File tree

3 files changed

+133
-0
lines changed

3 files changed

+133
-0
lines changed

solution/0200-0299/0285.Inorder Successor in BST/README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,52 @@
6464
```java
6565

6666
```
67+
### **JavaScript**
68+
69+
```js
70+
/**
71+
* Definition for a binary tree node.
72+
* function TreeNode(val) {
73+
* this.val = val;
74+
* this.left = this.right = null;
75+
* }
76+
*/
77+
/**
78+
* @param {TreeNode} root
79+
* @param {TreeNode} p
80+
* @return {TreeNode}
81+
*/
82+
var inorderSuccessor = function (root, p) {
83+
function findMin(root) {
84+
if (!root) {
85+
return null;
86+
}
87+
while (root.left) {
88+
root = root.left;
89+
}
90+
return root;
91+
}
92+
if (!root) {
93+
return null;
94+
}
95+
let successor = null;
96+
while (root) {
97+
if (root.val > p.val) {
98+
successor = root;
99+
root = root.left;
100+
} else if (root.val < p.val) {
101+
root = root.right;
102+
} else {
103+
if (root.right) {
104+
successor = findMin(root.right);
105+
}
106+
break;
107+
}
108+
}
109+
return successor;
110+
};
111+
```
112+
67113

68114
### **...**
69115

solution/0200-0299/0285.Inorder Successor in BST/README_EN.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,52 @@
5151

5252
```
5353

54+
### **JavaScript**
55+
56+
```js
57+
/**
58+
* Definition for a binary tree node.
59+
* function TreeNode(val) {
60+
* this.val = val;
61+
* this.left = this.right = null;
62+
* }
63+
*/
64+
/**
65+
* @param {TreeNode} root
66+
* @param {TreeNode} p
67+
* @return {TreeNode}
68+
*/
69+
var inorderSuccessor = function (root, p) {
70+
function findMin(root) {
71+
if (!root) {
72+
return null;
73+
}
74+
while (root.left) {
75+
root = root.left;
76+
}
77+
return root;
78+
}
79+
if (!root) {
80+
return null;
81+
}
82+
let successor = null;
83+
while (root) {
84+
if (root.val > p.val) {
85+
successor = root;
86+
root = root.left;
87+
} else if (root.val < p.val) {
88+
root = root.right;
89+
} else {
90+
if (root.right) {
91+
successor = findMin(root.right);
92+
}
93+
break;
94+
}
95+
}
96+
return successor;
97+
};
98+
```
99+
54100
### **...**
55101

56102
```
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
* @param {TreeNode} root
10+
* @param {TreeNode} p
11+
* @return {TreeNode}
12+
*/
13+
var inorderSuccessor = function (root, p) {
14+
function findMin(root) {
15+
if (!root) {
16+
return null;
17+
}
18+
while (root.left) {
19+
root = root.left;
20+
}
21+
return root;
22+
}
23+
if (!root) {
24+
return null;
25+
}
26+
let successor = null;
27+
while (root) {
28+
if (root.val > p.val) {
29+
successor = root;
30+
root = root.left;
31+
} else if (root.val < p.val) {
32+
root = root.right;
33+
} else {
34+
if (root.right) {
35+
successor = findMin(root.right);
36+
}
37+
break;
38+
}
39+
}
40+
return successor;
41+
};

0 commit comments

Comments
 (0)