题目描述
144. 二叉树的前序遍历
给你二叉树的根节点 root ,返回它节点值的 前序 **遍历。
示例 1:
输入: root = [1,null,2,3]
输出: [1,2,3]
示例 2:
输入: root = []
输出: []
示例 3:
输入: root = [1]
输出: [1]
示例 4:
输入: root = [1,2]
输出: [1,2]
示例 5:
输入: root = [1,null,2]
输出: [1,2]
提示:
- 树中节点数目在范围
[0, 100]内 -100 <= Node.val <= 100
进阶: 递归算法很简单,你可以通过迭代算法完成吗?
145. 二叉树的后序遍历
给你一棵二叉树的根节点 root ,返回其节点值的 后序遍历 。
示例 1:
输入: root = [1,null,2,3]
输出: [3,2,1]
示例 2:
输入: root = []
输出: []
示例 3:
输入: root = [1]
输出: [1]
提示:
- 树中节点的数目在范围
[0, 100]内 -100 <= Node.val <= 100
进阶: 递归算法很简单,你可以通过迭代算法完成吗?
94. 二叉树的中序遍历
给定一个二叉树的根节点 root ,返回 它的 中序 遍历 。
示例 1:
输入: root = [1,null,2,3]
输出: [1,3,2]
示例 2:
输入: root = []
输出: []
示例 3:
输入: root = [1]
输出: [1]
提示:
- 树中节点数目在范围
[0, 100]内 -100 <= Node.val <= 100
进阶: 递归算法很简单,你可以通过迭代算法完成吗?
分析解答
前序遍历(递归)
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number[]}
*/
var preorderTraversal = function(root, res = []) {
if (!root) return res
res.push(root.val)
preorderTraversal(root.left, res)
preorderTraversal(root.right, res)
return res
};
前序遍历(迭代)
/**
* 前序遍历
* @param {TreeNode} root
* @return {number[]}
*/
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;
};
后续遍历(递归)
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number[]}
*/
var postorderTraversal = function(root, res = []) {
if (!root) return res
postorderTraversal(root.left, res)
postorderTraversal(root.right, res)
res.push(root.val)
return res
};
后序遍历(迭代)
将迭代法的前序遍历先让 右先出,左后出,从而是 根右左。
最后再反转数组变成 左右根。
var postorderTraversal = function(root, res = []) {
// 简单的递归可以用栈来模拟 实现非递归(迭代)法遍历
if(!root) return res;
const stack = [root];
let cur = null;
while(stack.length) {
cur = stack.pop();
res.push(cur.val);
cur.left && stack.push(cur.left);
cur.right && stack.push(cur.right);
}
return res.reverse();
};
中序遍历(递归)
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number[]}
*/
var inorderTraversal = function(root, res = []) {
if (!root) return res
inorderTraversal(root.left, res)
res.push(root.val)
inorderTraversal(root.right, res)
return res
};
中序遍历(迭代)
中序遍历的迭代法和前后序遍历有所不同。
中序遍历是 左根右。思路是先一直遍历将 左子树加入到栈中。直到没有左子树。再将此时的子树加入到结果数组中。再去寻找判断右子树。重复进行这几个步骤。
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number[]}
*/
var inorderTraversal = function(root) {
const res = []
const stack = []
while (root != null || stack.length !== 0) {
while (root != null) {
stack.push(root)
root = root.left
}
root = stack.pop()
res.push(root.val)
root = root.right
}
return res
};