mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-11 04:54:51 +08:00
Merge pull request #1505 from w2xi/master
Update 0226.翻转二叉树.md JavaScript递归版本
This commit is contained in:
@ -470,25 +470,14 @@ func invertTree(root *TreeNode) *TreeNode {
|
|||||||
使用递归版本的前序遍历
|
使用递归版本的前序遍历
|
||||||
```javascript
|
```javascript
|
||||||
var invertTree = function(root) {
|
var invertTree = function(root) {
|
||||||
//1. 首先使用递归版本的前序遍历实现二叉树翻转
|
// 终止条件
|
||||||
//交换节点函数
|
if (!root) {
|
||||||
const inverNode=function(left,right){
|
return null;
|
||||||
let temp=left;
|
|
||||||
left=right;
|
|
||||||
right=temp;
|
|
||||||
//需要重新给root赋值一下
|
|
||||||
root.left=left;
|
|
||||||
root.right=right;
|
|
||||||
}
|
}
|
||||||
//确定递归函数的参数和返回值inverTree=function(root)
|
// 交换左右节点
|
||||||
//确定终止条件
|
const rightNode = root.right;
|
||||||
if(root===null){
|
root.right = invertTree(root.left);
|
||||||
return root;
|
root.left = invertTree(rightNode);
|
||||||
}
|
|
||||||
//确定节点处理逻辑 交换
|
|
||||||
inverNode(root.left,root.right);
|
|
||||||
invertTree(root.left);
|
|
||||||
invertTree(root.right);
|
|
||||||
return root;
|
return root;
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
@ -456,31 +456,42 @@ func deleteNode(root *TreeNode, key int) *TreeNode {
|
|||||||
* @param {number} key
|
* @param {number} key
|
||||||
* @return {TreeNode}
|
* @return {TreeNode}
|
||||||
*/
|
*/
|
||||||
var deleteNode = function (root, key) {
|
var deleteNode = function(root, key) {
|
||||||
if (root === null)
|
if (!root) return null;
|
||||||
return root;
|
if (key > root.val) {
|
||||||
if (root.val === key) {
|
|
||||||
if (!root.left)
|
|
||||||
return root.right;
|
|
||||||
else if (!root.right)
|
|
||||||
return root.left;
|
|
||||||
else {
|
|
||||||
let cur = root.right;
|
|
||||||
while (cur.left) {
|
|
||||||
cur = cur.left;
|
|
||||||
}
|
|
||||||
cur.left = root.left;
|
|
||||||
root = root.right;
|
|
||||||
delete root;
|
|
||||||
return root;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (root.val > key)
|
|
||||||
root.left = deleteNode(root.left, key);
|
|
||||||
if (root.val < key)
|
|
||||||
root.right = deleteNode(root.right, key);
|
root.right = deleteNode(root.right, key);
|
||||||
return root;
|
return root;
|
||||||
|
} else if (key < root.val) {
|
||||||
|
root.left = deleteNode(root.left, key);
|
||||||
|
return root;
|
||||||
|
} else {
|
||||||
|
// 场景1: 该节点是叶节点
|
||||||
|
if (!root.left && !root.right) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
// 场景2: 有一个孩子节点不存在
|
||||||
|
if (root.left && !root.right) {
|
||||||
|
return root.left;
|
||||||
|
} else if (root.right && !root.left) {
|
||||||
|
return root.right;
|
||||||
|
}
|
||||||
|
// 场景3: 左右节点都存在
|
||||||
|
const rightNode = root.right;
|
||||||
|
// 获取最小值节点
|
||||||
|
const minNode = getMinNode(rightNode);
|
||||||
|
// 将待删除节点的值替换为最小值节点值
|
||||||
|
root.val = minNode.val;
|
||||||
|
// 删除最小值节点
|
||||||
|
root.right = deleteNode(root.right, minNode.val);
|
||||||
|
return root;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
function getMinNode(root) {
|
||||||
|
while (root.left) {
|
||||||
|
root = root.left;
|
||||||
|
}
|
||||||
|
return root;
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
迭代
|
迭代
|
||||||
|
Reference in New Issue
Block a user