【前端er每日算法】二叉树-513找左下角的值 112路径总和 106从中序后序构造二叉树

94 阅读2分钟

题目一 513. 找树左下角的值

思路

左下角的值即最大深度的节点的第一个节点,所以记录maxDepth和node,当前遍历的节点的深度大于这个值时就更新node和maxDepth,遍历完成后,node即为要找的点。

var findBottomLeftValue = function(root) {
    let maxDepth = 0;
    let node = root;
    var find = (root, depth) => {
        if (!root) {
            return 0;
        }
        if (depth > maxDepth) {
            maxDepth = depth;
            node = root;
        }
        if (root.left) {
            find(root.left, depth + 1);
        }
        if (root.right) {
            find(root.right, depth + 1);
        }
    }
    find(root, 0);
    return node.val;
};

题目二 112. 路径总和

思路

终止条件:root为空,false,

如果找到叶子节点并且相减后==0,则说明找到了一条路径,返回true,

之后遍历左子树和右子树,获取返回值,只要有一个找到了,就找到了,直接返回。

最后都没有符合的返回false

var hasPathSum = function(root, targetSum) {
    if (!root) {
        return false;
    }
    if (!root.left && !root.right && targetSum - root.val === 0) {
        return true;
    }
    if (root.left) {
        const leftResult = hasPathSum(root.left, targetSum - root.val);
        if (leftResult) {
            return true;
        }
    }
    if (root.right) {
        const rightResult = hasPathSum(root.right, targetSum - root.val);
        if (rightResult) {
            return true;
        }
    }
    return false;
};

题目三 113. 路径总和 II

思路

做过回溯后,发现这个题就比较容易写出来了。

终止条件:root为空

单层逻辑:

  • root放入path中,如果到达叶子节点,并且和为0,则将path添加到结果集中。
  • 存在左子树,遍历左子树,将和减去root的值,处理完左子树后,将左子树的值pop。
  • 存在右子树,遍历右子树,将和减去root的值,处理完右子树后,将右子树的值pop。
/**
 * 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
 * @param {number} targetSum
 * @return {number[][]}
 */
var pathSum = function(root, targetSum) {
    let result = [];
    let path = [];
    var getPath = (root, targetSum) => {
        if (!root) {
            return;
        }
        path.push(root.val);
        if (!root.left && !root.right && targetSum - root.val === 0) {
            result.push(path.slice());
            return;
        }
        if (root.left) {
            getPath(root.left, targetSum - root.val);
            path.pop();
        }
        if (root.right) {
            getPath(root.right, targetSum - root.val);
            path.pop();
        }
    }
    getPath(root, targetSum);
    return result;
};

题目四 106. 从中序与后序遍历序列构造二叉树

思路

  • 构造根元素
  • 如果数组长度等于0,则返回null
  • 如果等于1,直接返回当前构造的节点
  • 否则,后续最后一个数即为根元素,找到在中序遍历中的位置,然后分割左侧中序数组,左侧后续数组,后续数组的要点是根据中序数组左侧数组的长度来切割。从而构造出root的左右子树。
var buildTree = function(inorder, postorder) {
    let len = postorder.length;
    if (len === 0) {
        return null;
    }
    let rootVal = postorder[len - 1];
    let root = new TreeNode(rootVal);
    if (len === 1) {
        return root;
    }
    let mid = 0;
    for (let i = 0; i < len; i++) {
        if (inorder[i] === rootVal) {
            mid = i;
            break;
        }
    }
    root.left = buildTree(inorder.slice(0, mid), postorder.slice(0, mid));
    root.right = buildTree(inorder.slice(mid+1, len), postorder.slice(mid, len - 1));
    return root;
};

// 2023.8.24 又写了一遍,是不是清晰多了
var buildTree = function(inorder, postorder) {
    if (!inorder.length) {
        return null
    }
    if (inorder.length === 1) {
        return new TreeNode(inorder[0]);
    }
    const rootVal = postorder[postorder.length - 1];
    const root = new TreeNode(rootVal);
    const rootIndex = inorder.indexOf(rootVal, inorder, postorder);
    root.left = buildTree(inorder.slice(0, rootIndex), postorder.slice(0, rootIndex));
    root.right = buildTree(inorder.slice(rootIndex + 1), postorder.slice(rootIndex, -1));
    return root;
};