mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
二叉树的递归遍历JavaScript版本
This commit is contained in:
@ -306,6 +306,59 @@ var postorderTraversal = function(root, res = []) {
|
|||||||
return res;
|
return res;
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
Javascript版本:
|
||||||
|
|
||||||
|
前序遍历:
|
||||||
|
```Javascript
|
||||||
|
var preorderTraversal = function(root) {
|
||||||
|
let res=[];
|
||||||
|
const dfs=function(root){
|
||||||
|
if(root===null)return ;
|
||||||
|
//先序遍历所以从父节点开始
|
||||||
|
res.push(root.val);
|
||||||
|
//递归左子树
|
||||||
|
dfs(root.left);
|
||||||
|
//递归右子树
|
||||||
|
dfs(root.right);
|
||||||
|
}
|
||||||
|
//只使用一个参数 使用闭包进行存储结果
|
||||||
|
dfs(root);
|
||||||
|
return res;
|
||||||
|
};
|
||||||
|
```
|
||||||
|
中序遍历
|
||||||
|
```javascript
|
||||||
|
var inorderTraversal = function(root) {
|
||||||
|
let res=[];
|
||||||
|
const dfs=function(root){
|
||||||
|
if(root===null){
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
dfs(root.left);
|
||||||
|
res.push(root.val);
|
||||||
|
dfs(root.right);
|
||||||
|
}
|
||||||
|
dfs(root);
|
||||||
|
return res;
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
后序遍历
|
||||||
|
```javascript
|
||||||
|
var postorderTraversal = function(root) {
|
||||||
|
let res=[];
|
||||||
|
const dfs=function(root){
|
||||||
|
if(root===null){
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
dfs(root.left);
|
||||||
|
dfs(root.right);
|
||||||
|
res.push(root.val);
|
||||||
|
}
|
||||||
|
dfs(root);
|
||||||
|
return res;
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user