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

Commit93db5a0

Browse files
Merge pull requestyoungyangyang04#441 from kok-s0s/master
提供二叉树部分的JavaScript代码
2 parentscd4be47 +d5a656a commit93db5a0

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed

‎problems/0450.删除二叉搜索树中的节点.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,51 @@ func deleteNode1(root *TreeNode)*TreeNode{
359359
}
360360
```
361361

362+
JavaScript版本
363+
364+
> 递归
365+
366+
```javascript
367+
/**
368+
* Definition for a binary tree node.
369+
* function TreeNode(val, left, right) {
370+
* this.val = (val===undefined ? 0 : val)
371+
* this.left = (left===undefined ? null : left)
372+
* this.right = (right===undefined ? null : right)
373+
* }
374+
*/
375+
/**
376+
* @param {TreeNode} root
377+
* @param {number} key
378+
* @return {TreeNode}
379+
*/
380+
var deleteNode = function (root, key) {
381+
if (root === null)
382+
return root;
383+
if (root.val === key) {
384+
if (!root.left)
385+
return root.right;
386+
else if (!root.right)
387+
return root.left;
388+
else {
389+
let cur = root.right;
390+
while (cur.left) {
391+
cur = cur.left;
392+
}
393+
cur.left = root.left;
394+
let temp = root;
395+
root = root.right;
396+
delete root;
397+
return root;
398+
}
399+
}
400+
if (root.val > key)
401+
root.left = deleteNode(root.left, key);
402+
if (root.val < key)
403+
root.right = deleteNode(root.right, key);
404+
return root;
405+
};
406+
```
362407

363408

364409

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

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,84 @@ var insertIntoBST = function (root, val) {
320320
};
321321
```
322322

323+
> 无返回值的递归
323324

325+
```javascript
326+
/**
327+
* Definition for a binary tree node.
328+
* function TreeNode(val, left, right) {
329+
* this.val = (val===undefined ? 0 : val)
330+
* this.left = (left===undefined ? null : left)
331+
* this.right = (right===undefined ? null : right)
332+
* }
333+
*/
334+
/**
335+
* @param {TreeNode} root
336+
* @param {number} val
337+
* @return {TreeNode}
338+
*/
339+
var insertIntoBST = function (root, val) {
340+
let parent = new TreeNode(0);
341+
const preOrder = (cur, val) => {
342+
if (cur === null) {
343+
let node = new TreeNode(val);
344+
if (parent.val > val)
345+
parent.left = node;
346+
else
347+
parent.right = node;
348+
return;
349+
}
350+
parent = cur;
351+
if (cur.val > val)
352+
preOrder(cur.left, val);
353+
if (cur.val < val)
354+
preOrder(cur.right, val);
355+
}
356+
if (root === null)
357+
root = new TreeNode(val);
358+
preOrder(root, val);
359+
return root;
360+
};
361+
```
362+
363+
>迭代
364+
365+
```javascript
366+
/**
367+
* Definition for a binary tree node.
368+
* function TreeNode(val, left, right) {
369+
* this.val = (val===undefined ? 0 : val)
370+
* this.left = (left===undefined ? null : left)
371+
* this.right = (right===undefined ? null : right)
372+
* }
373+
*/
374+
/**
375+
*@param{TreeNode}root
376+
*@param{number}val
377+
*@return{TreeNode}
378+
*/
379+
varinsertIntoBST=function (root,val) {
380+
if (root===null) {
381+
root=newTreeNode(val);
382+
}else {
383+
let parent=newTreeNode(0);
384+
let cur= root;
385+
while (cur) {
386+
parent= cur;
387+
if (cur.val> val)
388+
cur=cur.left;
389+
else
390+
cur=cur.right;
391+
}
392+
let node=newTreeNode(val);
393+
if (parent.val> val)
394+
parent.left= node;
395+
else
396+
parent.right= node;
397+
}
398+
return root;
399+
};
400+
```
324401

325402
-----------------------
326403
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp