Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit806e72f

Browse files
Merge pull requestyoungyangyang04#436 from kok-s0s/master
提供二叉树部分的JavaScript代码
2 parentsf6b6adb +826927e commit806e72f

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

‎problems/0235.二叉搜索树的最近公共祖先.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,62 @@ func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
314314
```
315315

316316

317+
JavaScript版本
318+
>递归
317319
320+
```javascript
321+
/**
322+
* Definition for a binary tree node.
323+
* function TreeNode(val) {
324+
* this.val = val;
325+
* this.left = this.right = null;
326+
* }
327+
*/
328+
329+
/**
330+
*@param{TreeNode}root
331+
*@param{TreeNode}p
332+
*@param{TreeNode}q
333+
*@return{TreeNode}
334+
*/
335+
varlowestCommonAncestor=function(root,p,q) {
336+
if(root.val>p.val&&root.val>q.val)
337+
returnlowestCommonAncestor(root.left, p , q);
338+
elseif(root.val<p.val&&root.val<q.val)
339+
returnlowestCommonAncestor(root.right, p , q);
340+
return root;
341+
};
342+
```
343+
344+
>迭代
345+
346+
```javascript
347+
/**
348+
* Definition for a binary tree node.
349+
* function TreeNode(val) {
350+
* this.val = val;
351+
* this.left = this.right = null;
352+
* }
353+
*/
354+
355+
/**
356+
*@param{TreeNode}root
357+
*@param{TreeNode}p
358+
*@param{TreeNode}q
359+
*@return{TreeNode}
360+
*/
361+
varlowestCommonAncestor=function(root,p,q) {
362+
while(1) {
363+
if(root.val>p.val&&root.val>q.val)
364+
root=root.left;
365+
elseif(root.val<p.val&&root.val<q.val)
366+
root=root.right;
367+
else
368+
break;
369+
}
370+
return root;
371+
};
372+
```
318373

319374

320375
-----------------------

‎problems/0701.二叉搜索树中的插入操作.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,39 @@ func insertIntoBST(root *TreeNode, val int) *TreeNode {
286286
}
287287
```
288288

289+
JavaScript版本
290+
291+
> 有返回值的递归写法
292+
293+
```javascript
294+
/**
295+
* Definition for a binary tree node.
296+
* function TreeNode(val, left, right) {
297+
* this.val = (val===undefined ? 0 : val)
298+
* this.left = (left===undefined ? null : left)
299+
* this.right = (right===undefined ? null : right)
300+
* }
301+
*/
302+
/**
303+
* @param {TreeNode} root
304+
* @param {number} val
305+
* @return {TreeNode}
306+
*/
307+
var insertIntoBST = function (root, val) {
308+
const setInOrder = (root, val) => {
309+
if (root === null) {
310+
let node = new TreeNode(val);
311+
return node;
312+
}
313+
if (root.val > val)
314+
root.left = setInOrder(root.left, val);
315+
else if (root.val < val)
316+
root.right = setInOrder(root.right, val);
317+
return root;
318+
}
319+
return setInOrder(root, val);
320+
};
321+
```
289322

290323

291324

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp