mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
0226.翻转二叉树的JavaScript版本
This commit is contained in:
@ -247,10 +247,100 @@ func invertTree(root *TreeNode) *TreeNode {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
JavaScript:
|
||||||
|
|
||||||
|
使用递归版本的前序遍历
|
||||||
|
```javascript
|
||||||
|
var invertTree = function(root) {
|
||||||
|
//1. 首先使用递归版本的前序遍历实现二叉树翻转
|
||||||
|
//交换节点函数
|
||||||
|
const inverNode=function(left,right){
|
||||||
|
let temp=left;
|
||||||
|
left=right;
|
||||||
|
right=temp;
|
||||||
|
//需要重新给root赋值一下
|
||||||
|
root.left=left;
|
||||||
|
root.right=right;
|
||||||
|
}
|
||||||
|
//确定递归函数的参数和返回值inverTree=function(root)
|
||||||
|
//确定终止条件
|
||||||
|
if(root===null){
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
//确定节点处理逻辑 交换
|
||||||
|
inverNode(root.left,root.right);
|
||||||
|
invertTree(root.left);
|
||||||
|
invertTree(root.right);
|
||||||
|
return root;
|
||||||
|
};
|
||||||
|
```
|
||||||
|
使用迭代版本(统一模板))的前序遍历:
|
||||||
|
```javascript
|
||||||
|
var invertTree = function(root) {
|
||||||
|
//我们先定义节点交换函数
|
||||||
|
const invertNode=function(root,left,right){
|
||||||
|
let temp=left;
|
||||||
|
left=right;
|
||||||
|
right=temp;
|
||||||
|
root.left=left;
|
||||||
|
root.right=right;
|
||||||
|
}
|
||||||
|
//使用迭代方法的前序遍历
|
||||||
|
let stack=[];
|
||||||
|
if(root===null){
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
stack.push(root);
|
||||||
|
while(stack.length){
|
||||||
|
let node=stack.pop();
|
||||||
|
if(node!==null){
|
||||||
|
//前序遍历顺序中左右 入栈顺序是前序遍历的倒序右左中
|
||||||
|
node.right&&stack.push(node.right);
|
||||||
|
node.left&&stack.push(node.left);
|
||||||
|
stack.push(node);
|
||||||
|
stack.push(null);
|
||||||
|
}else{
|
||||||
|
node=stack.pop();
|
||||||
|
//节点处理逻辑
|
||||||
|
invertNode(node,node.left,node.right);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return root;
|
||||||
|
};
|
||||||
|
```
|
||||||
|
使用层序遍历:
|
||||||
|
```javascript
|
||||||
|
var invertTree = function(root) {
|
||||||
|
//我们先定义节点交换函数
|
||||||
|
const invertNode=function(root,left,right){
|
||||||
|
let temp=left;
|
||||||
|
left=right;
|
||||||
|
right=temp;
|
||||||
|
root.left=left;
|
||||||
|
root.right=right;
|
||||||
|
}
|
||||||
|
//使用层序遍历
|
||||||
|
let queue=[];
|
||||||
|
if(root===null){
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
queue.push(root);
|
||||||
|
while(queue.length){
|
||||||
|
let length=queue.length;
|
||||||
|
while(length--){
|
||||||
|
let node=queue.shift();
|
||||||
|
//节点处理逻辑
|
||||||
|
invertNode(node,node.left,node.right);
|
||||||
|
node.left&&queue.push(node.left);
|
||||||
|
node.right&&queue.push(node.right);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return root;
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
||||||
|
Reference in New Issue
Block a user