Skip to content

Commit aa17c24

Browse files
committed
feat: add solutions to lcci problem: No.04.06
04.06.Successor
1 parent fcf4ced commit aa17c24

File tree

3 files changed

+220
-0
lines changed

3 files changed

+220
-0
lines changed

lcci/04.06.Successor/README.md

+98
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737

3838
<!-- 这里可写通用的实现逻辑 -->
3939

40+
41+
4042
<!-- tabs:start -->
4143

4244
### **Python3**
@@ -143,6 +145,39 @@ public:
143145
};
144146
```
145147
148+
```cpp
149+
/**
150+
* Definition for a binary tree node.
151+
* struct TreeNode {
152+
* int val;
153+
* TreeNode *left;
154+
* TreeNode *right;
155+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
156+
* };
157+
*/
158+
class Solution {
159+
public:
160+
TreeNode *inorderSuccessor(TreeNode *root, TreeNode *p) {
161+
stack<TreeNode *> stk;
162+
TreeNode *cur = root;
163+
while (cur != nullptr || !stk.empty()) {
164+
if (cur == nullptr) {
165+
cur = stk.top();
166+
stk.pop();
167+
if (cur->val > p->val) {
168+
return cur;
169+
}
170+
cur = cur->right;
171+
} else {
172+
stk.push(cur);
173+
cur = cur->left;
174+
}
175+
}
176+
return cur;
177+
}
178+
};
179+
```
180+
146181
### **Go**
147182

148183
```go
@@ -173,6 +208,69 @@ func inorderSuccessor(root *TreeNode, p *TreeNode) *TreeNode {
173208
}
174209
```
175210

211+
### **JavaScript**
212+
213+
```js
214+
/**
215+
* Definition for a binary tree node.
216+
* function TreeNode(val) {
217+
* this.val = val;
218+
* this.left = this.right = null;
219+
* }
220+
*/
221+
/**
222+
* @param {TreeNode} root
223+
* @param {TreeNode} p
224+
* @return {TreeNode}
225+
*/
226+
var inorderSuccessor = function (root, p) {
227+
if (root == null) {
228+
return root
229+
}
230+
const { val, left, right } = root
231+
const res = inorderSuccessor(left, p)
232+
if (res != null) {
233+
return res
234+
}
235+
if (val > p.val) {
236+
return root
237+
}
238+
return inorderSuccessor(right, p)
239+
};
240+
```
241+
242+
```js
243+
/**
244+
* Definition for a binary tree node.
245+
* function TreeNode(val) {
246+
* this.val = val;
247+
* this.left = this.right = null;
248+
* }
249+
*/
250+
/**
251+
* @param {TreeNode} root
252+
* @param {TreeNode} p
253+
* @return {TreeNode}
254+
*/
255+
var inorderSuccessor = function (root, p) {
256+
const stack = [];
257+
let cur = root;
258+
while (cur != null || stack.length !== 0) {
259+
if (cur == null) {
260+
cur = stack.pop();
261+
if (cur.val > p.val) {
262+
return cur;
263+
}
264+
cur = cur.right;
265+
} else {
266+
stack.push(cur);
267+
cur = cur.left;
268+
}
269+
}
270+
return cur;
271+
};
272+
```
273+
176274
### **...**
177275

178276
```

lcci/04.06.Successor/README_EN.md

+96
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,39 @@ public:
156156
};
157157
```
158158
159+
```cpp
160+
/**
161+
* Definition for a binary tree node.
162+
* struct TreeNode {
163+
* int val;
164+
* TreeNode *left;
165+
* TreeNode *right;
166+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
167+
* };
168+
*/
169+
class Solution {
170+
public:
171+
TreeNode *inorderSuccessor(TreeNode *root, TreeNode *p) {
172+
stack<TreeNode *> stk;
173+
TreeNode *cur = root;
174+
while (cur != nullptr || !stk.empty()) {
175+
if (cur == nullptr) {
176+
cur = stk.top();
177+
stk.pop();
178+
if (cur->val > p->val) {
179+
return cur;
180+
}
181+
cur = cur->right;
182+
} else {
183+
stk.push(cur);
184+
cur = cur->left;
185+
}
186+
}
187+
return cur;
188+
}
189+
};
190+
```
191+
159192
### **Go**
160193

161194
```go
@@ -186,6 +219,69 @@ func inorderSuccessor(root *TreeNode, p *TreeNode) *TreeNode {
186219
}
187220
```
188221

222+
### **JavaScript**
223+
224+
```js
225+
/**
226+
* Definition for a binary tree node.
227+
* function TreeNode(val) {
228+
* this.val = val;
229+
* this.left = this.right = null;
230+
* }
231+
*/
232+
/**
233+
* @param {TreeNode} root
234+
* @param {TreeNode} p
235+
* @return {TreeNode}
236+
*/
237+
var inorderSuccessor = function (root, p) {
238+
if (root == null) {
239+
return root
240+
}
241+
const { val, left, right } = root
242+
const res = inorderSuccessor(left, p)
243+
if (res != null) {
244+
return res
245+
}
246+
if (val > p.val) {
247+
return root
248+
}
249+
return inorderSuccessor(right, p)
250+
};
251+
```
252+
253+
```js
254+
/**
255+
* Definition for a binary tree node.
256+
* function TreeNode(val) {
257+
* this.val = val;
258+
* this.left = this.right = null;
259+
* }
260+
*/
261+
/**
262+
* @param {TreeNode} root
263+
* @param {TreeNode} p
264+
* @return {TreeNode}
265+
*/
266+
var inorderSuccessor = function (root, p) {
267+
const stack = [];
268+
let cur = root;
269+
while (cur != null || stack.length !== 0) {
270+
if (cur == null) {
271+
cur = stack.pop();
272+
if (cur.val > p.val) {
273+
return cur;
274+
}
275+
cur = cur.right;
276+
} else {
277+
stack.push(cur);
278+
cur = cur.left;
279+
}
280+
}
281+
return cur;
282+
};
283+
```
284+
189285
### **...**
190286

191287
```

lcci/04.06.Successor/Solution.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
if (root == null) {
15+
return root;
16+
}
17+
const { val, left, right } = root;
18+
const res = inorderSuccessor(left, p);
19+
if (res != null) {
20+
return res;
21+
}
22+
if (val > p.val) {
23+
return root;
24+
}
25+
return inorderSuccessor(right, p);
26+
};

0 commit comments

Comments
 (0)