mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 11:34:46 +08:00
二叉树的统一迭代法 javaScript版本
This commit is contained in:
@ -374,6 +374,86 @@ func postorderTraversal(root *TreeNode) []int {
|
||||
}
|
||||
```
|
||||
|
||||
javaScript:
|
||||
|
||||
> 前序遍历统一迭代法
|
||||
|
||||
```js
|
||||
|
||||
// 前序遍历:中左右
|
||||
// 压栈顺序:右左中
|
||||
|
||||
var preorderTraversal = function(root, res = []) {
|
||||
const stack = [];
|
||||
if (root) stack.push(root);
|
||||
while(stack.length) {
|
||||
const node = stack.pop();
|
||||
if(!node) {
|
||||
res.push(stack.pop().val);
|
||||
continue;
|
||||
}
|
||||
if (node.right) stack.push(node.right); // 右
|
||||
if (node.left) stack.push(node.left); // 左
|
||||
stack.push(node); // 中
|
||||
stack.push(null);
|
||||
};
|
||||
return res;
|
||||
};
|
||||
|
||||
```
|
||||
|
||||
> 中序遍历统一迭代法
|
||||
|
||||
```js
|
||||
|
||||
// 中序遍历:左中右
|
||||
// 压栈顺序:右中左
|
||||
|
||||
var inorderTraversal = function(root, res = []) {
|
||||
const stack = [];
|
||||
if (root) stack.push(root);
|
||||
while(stack.length) {
|
||||
const node = stack.pop();
|
||||
if(!node) {
|
||||
res.push(stack.pop().val);
|
||||
continue;
|
||||
}
|
||||
if (node.right) stack.push(node.right); // 右
|
||||
stack.push(node); // 中
|
||||
stack.push(null);
|
||||
if (node.left) stack.push(node.left); // 左
|
||||
};
|
||||
return res;
|
||||
};
|
||||
|
||||
```
|
||||
|
||||
> 后序遍历统一迭代法
|
||||
|
||||
```js
|
||||
|
||||
// 后续遍历:左右中
|
||||
// 压栈顺序:中右左
|
||||
|
||||
var postorderTraversal = function(root, res = []) {
|
||||
const stack = [];
|
||||
if (root) stack.push(root);
|
||||
while(stack.length) {
|
||||
const node = stack.pop();
|
||||
if(!node) {
|
||||
res.push(stack.pop().val);
|
||||
continue;
|
||||
}
|
||||
stack.push(node); // 中
|
||||
stack.push(null);
|
||||
if (node.right) stack.push(node.right); // 右
|
||||
if (node.left) stack.push(node.left); // 左
|
||||
};
|
||||
return res;
|
||||
};
|
||||
|
||||
```
|
||||
|
||||
|
||||
|
||||
-----------------------
|
||||
|
Reference in New Issue
Block a user