自学内容网 自学内容网

代码随想录算法训练营第16天| 513. 找树左下角的值, 112. 路径总和, 106. 从中序与后序遍历序列构造二叉树

学习任务:


Leetcode513. 找树左下角的值

难度:中等 | 相关标签:树、深度优先搜索、广度优先搜索、二叉树

  • 题目: 给定一个二叉树的 根节点 root,请找出该二叉树的 最底层 最左边 节点的值。
    假设二叉树中至少有一个节点。

  • 思路: 要找出树的最后一行的最左边的值。此时大家应该想起用层序遍历是非常简单的了,反而用递归的话会比较难一点。

  • 注意:

    • 使用递归法,如何判断是最后一行呢,其实就是深度最大的叶子节点一定是最后一行
    • 使用迭代法,只需要记录最后一行第一个节点的数值就可以了。
  • 代码:
    迭代法

class Solution {

    public int findBottomLeftValue(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        int res = 0;
        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                TreeNode poll = queue.poll();
                if (i == 0) {
                    res = poll.val;
                }
                if (poll.left != null) {
                    queue.offer(poll.left);
                }
                if (poll.right != null) {
                    queue.offer(poll.right);
                }
            }
        }
        return res;
    }
}
`递归法`
class Solution {
    private int Deep = -1;
    private int value = 0;
    public int findBottomLeftValue(TreeNode root) {
        value = root.val;
        findLeftValue(root,0);
        return value;
    }

    private void findLeftValue (TreeNode root,int deep) {
        if (root == null) return;
        if (root.left == null && root.right == null) {
            if (deep > Deep) {
                value = root.val;
                Deep = deep;
            }
        }
        if (root.left != null) findLeftValue(root.left,deep + 1);
        if (root.right != null) findLeftValue(root.right,deep + 1);
    }
}
  • 反思:

Leetcode112. 路径总和

难度:简单 | 相关标签:树、深度优先搜索、广度优先搜索、二叉树

  • 题目: 给你二叉树的根节点 root 和一个表示目标和的整数 targetSum 。判断该树中是否存在 根节点到叶子节点 的路径,这条路径上所有节点值相加等于目标和 targetSum 。如果存在,返回 true ;否则,返回 false 。
    叶子节点 是指没有子节点的节点。

  • 思路: 利用深度优先搜索(DFS)的思想,通过递归遍历二叉树的每个节点,并在遍历过程中不断更新目标和。一旦找到匹配的路径或确定不存在匹配的路径,递归将返回结果。

  • 注意:

    • 递归函数什么时候需要返回值?什么时候不需要返回值?这里总结如下三点:
      • 如果需要搜索整棵二叉树且不用处理递归返回值,递归函数就不要返回值。
      • 如果需要搜索整棵二叉树且需要处理递归返回值,递归函数就需要返回值。
      • 如果要搜索其中一条符合条件的路径,那么递归一定需要返回值,因为遇到符合条件的路径了就要及时返回。
  • 代码:

class Solution {
    public boolean hasPathSum(TreeNode root, int targetSum) {
        // 如果根节点为空,直接返回 false
        if (root == null) {
            return false;
        }
        // 检查当前节点是否是叶子节点
        targetSum = targetSum - root.val;
        if(root.left == null && root.right == null ){
            return  targetSum == 0;
        }
        // 递归检查左右子树
        if(root.left != null){
            if( hasPathSum(root.left, targetSum)){
                return true;
            }
        }
        if(root.right != null){
            if( hasPathSum(root.right, targetSum)){
                return true;
            }
        }
        return false;
    }
}
  • 反思:

Leetcode106. 从中序与后序遍历序列构造二叉树

难度:简单 | 相关标签:树、数组、哈希表、分治、二叉树

  • 题目: 给定两个整数数组 inorder 和 postorder ,其中 inorder 是二叉树的中序遍历, postorder 是同一棵树的后序遍历,请你构造并返回这颗 二叉树 。

  • 思路:

    • 第一步:如果数组大小为零的话,说明是空节点了。
    • 第二步:如果不为空,那么取后序数组最后一个元素作为节点元素。
    • 第三步:找到后序数组最后一个元素在中序数组的位置,作为切割点
    • 第四步:切割中序数组,切成中序左数组和中序右数组 (顺序别搞反了,一定是先切中序数组)
    • 第五步:切割后序数组,切成后序左数组和后序右数组
    • 第六步:递归处理左区间和右区间
  • 注意:

    • 前序和后序不能唯一确定一棵二叉树!,因为没有中序遍历无法确定左右部分,也就是无法分割。
  • 代码: 写不出来

class Solution {
    Map<Integer, Integer> map;  // 方便根据数值查找位置
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        map = new HashMap<>();
        for (int i = 0; i < inorder.length; i++) { // 用map保存中序序列的数值对应位置
            map.put(inorder[i], i);
        }

        return findNode(inorder,  0, inorder.length, postorder,0, postorder.length);  // 前闭后开
    }

    public TreeNode findNode(int[] inorder, int inBegin, int inEnd, int[] postorder, int postBegin, int postEnd) {
        // 参数里的范围都是前闭后开
        if (inBegin >= inEnd || postBegin >= postEnd) {  // 不满足左闭右开,说明没有元素,返回空树
            return null;
        }
        int rootIndex = map.get(postorder[postEnd - 1]);  // 找到后序遍历的最后一个元素在中序遍历中的位置
        TreeNode root = new TreeNode(inorder[rootIndex]);  // 构造结点
        int lenOfLeft = rootIndex - inBegin;  // 保存中序左子树个数,用来确定后序数列的个数
        root.left = findNode(inorder, inBegin, rootIndex,
                            postorder, postBegin, postBegin + lenOfLeft);
        root.right = findNode(inorder, rootIndex + 1, inEnd,
                            postorder, postBegin + lenOfLeft, postEnd - 1);

        return root;
    }
}
  • 反思:

原文地址:https://blog.csdn.net/qq_44932556/article/details/140506792

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!