二叉树遍历
递归遍历
res = []
function traversal(node){
if(node == null) return
//前序遍历 中-左右
res.push(node.value)
traversal(node.left)
traversal(node.right)
}
function traversal(node){
if(node == null) return
// 中序遍历 左中右
traversal(node.left)
res.push(node.value)
traversal(node.right)
}
function traversal(node){
if(node == null) return
// 后序遍历 左右中
traversal(node.left)
traversal(node.right)
res.push(node.value)
}
迭代遍历
var preorderTraversal = function(root, res = []) {
if(!root) return res;
const stack = [root];
let cur = null;
while(stack.length) {
cur = stack.pop();
res.push(cur.val);
cur.right && stack.push(cur.right);
cur.left && stack.push(cur.left);
}
return res;
};
中序遍历:
// 入栈 左 -> 右
// 出栈 左 -> 中 -> 右
var inorderTraversal = function(root, res = []) {
const stack = [];
let cur = root;
while(stack.length || cur) {
if(cur) {
stack.push(cur);
// 左
cur = cur.left;
} else {
// --> 弹出 中
cur = stack.pop();
res.push(cur.val);
// 右
cur = cur.right;
}
};
return res;
};
后序遍历:
// 入栈 左 -> 右
// 出栈 中 -> 右 -> 左 结果翻转
var postorderTraversal = function(root, res = []) {
if (!root) return res;
const stack = [root];
let cur = null;
do {
cur = stack.pop();
res.push(cur.val);
cur.left && stack.push(cur.left);
cur.right && stack.push(cur.right);
} while(stack.length);
return res.reverse();
};