代码随想录算法训练营第14天 【二叉树】| 144. 二叉树的前序遍历 145. 二叉树的后序遍历 94. 二叉树的中序遍历

82 阅读2分钟

几个概念

满二叉树 2^k - 1

完全二叉树 只有底层可以空缺,但必须从左到右连续

二叉搜索树左子树val小于根节点,右子树val大于根节点

平衡二叉搜索树 左子树与右子树高度相差 < 2

1.前序144. 二叉树的前序遍历

递归 (方位指的是中节点的文字)

/**
 * 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) {
    let res = [];
    // 前序遍历 中→左→右
    const dfs = function(root) {
        if (root === null) return;
        res.push(root.val);
        dfs(root.left);
        dfs(root.right);
    }
    dfs(root);
    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 preorderTraversal = function(root) {
    let res = [];
    if (root === null) 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 preorderTraversal = function(root) {
    let res = [];
    const stack =[];
    if (root) {
        stack.push(root);
    }
    while (stack.length) {
        // 取出当前栈顶元素
        let node = stack.pop();
        // 为空处理后一个节点,取值输出
        if (!node) {
            res.push(stack.pop().val);
            continue;
        }
        // 倒序入栈: 右 左 中
        node.right && stack.push(node.right);
        node.left && stack.push(node.left);
        stack.push(node);
        stack.push(null);
    }
    return res;
};

2.中序94. 二叉树的中序遍历

递归

/**
 * 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) {
    let res = [];
    const dfs = function(root) {
        if (root === null) return;
        dfs(root.left);
        res.push(root.val);
        dfs(root.right);
    }
    dfs(root);
    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) {
    let res = [];
    let stack = [];
    while (stack.length || root) {
        // 左
        while (root) {
            stack.push(root);
            root = root.left;
        }
        // 中
        root = stack.pop();
        res.push(root.val);
        // 右
        root = root.right;
    }
    return res;
};

3.后序145. 二叉树的后序遍历

递归

/**
 * 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) {
    let res = [];
    const dfs = function(root) {
        if (root === null) return;
        dfs(root.left);
        dfs(root.right);
        res.push(root.val);
    }
    dfs(root);
    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) {
    let res = [];
    if (!root) return res;
    let 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();
};