(二刷)代码随想录第18天|513.找树左下角的值● 112. 路径总和 113.路径总和ii● 106.从中序与后序遍历序列构造二叉树 105.从前序与中序遍历序列构造二叉树

5月底至6月中旬一直都在复习(预习)期末考试,所以没刷题,6.17-6.27小学期,学校安排了课程和作业。

513.找树左下角的值

513. 找树左下角的值 - 力扣(LeetCode)

这个题一眼就是层序遍历,但是在自己写的过程中有一个疑惑:层序遍历的过程中怎么确定二叉树的一层已经遍历完了呢?查看了代码随想录的代码,询问了gpt:

综合代码:

// 定义一个名为 Solution 的类
class Solution {
    
    // 定义一个公共方法 findBottomLeftValue,它接受一个 TreeNode 类型的参数 root,并返回一个整数
    public int findBottomLeftValue(TreeNode root) {
        // 创建一个队列来保存 TreeNode 节点,使用 LinkedList 实现
        Queue<TreeNode> queue = new LinkedList<>();
        // 将根节点添加到队列中
        queue.offer(root);
        // 定义一个变量 res 来存储最终的结果,初始值为 0
        int res = 0;
        // 当队列不为空时,执行循环
        while (!queue.isEmpty()) {
            // 获取当前队列的大小,即当前层的节点数量
            int size = queue.size();
            // 遍历当前层的所有节点
            for (int i = 0; i < size; i++) {
                // 从队列中取出一个节点
                TreeNode poll = queue.poll();
                // 如果这是当前层的第一个节点,更新 res 为该节点的值
                if (i == 0) {
                    res = poll.val;
                }
                // 如果该节点有左子节点,则将左子节点加入队列
                if (poll.left != null) {
                    queue.offer(poll.left);
                }
                // 如果该节点有右子节点,则将右子节点加入队列
                if (poll.right != null) {
                    queue.offer(poll.right);
                }
            }
        }
        // 返回最终的结果,即二叉树最底层最左边节点的值
        return res;
    }
}
  • 112. 路径总和

112. 路径总和 - 力扣(LeetCode)

一眼就是要用深度优先遍历,最开始自己选定深度优先之后一下子没想起来递归三部曲。

class Solution {
    public boolean hasPathSum(TreeNode root, int targetSum) {
        //确定参数和返回值

        //确定终止条件
        if(root == null){
            return false;
        }
        targetSum -= root.val;

        if(root.left == null && root.right == null){
            return targetSum == 0;
        }
        if(root.left != null){
            boolean left = hasPathSum(root.left, targetSum);
            if(left){
                return true;
            }
        }

        if(root.right != null){
            boolean right = hasPathSum(root.right, targetSum);
            if(right){
                return true;
            }
        }

        return false;
    }
}
  • 106.从中序与后序遍历序列构造二叉树

用递归方法一层一层切割:
1、如果数组大小为0,说明为空节点;

2、如果不为空,取后序数组最后一个元素作为节点元素;

3、找到节点元素在中序数组中的位置,作为切割点

4、切割中序数组,切成中序左数组和中序右数组;

5、切割后序数组,切成后序左数组和后序右数组;

6、递归处理左区间和右区间;

综合代码:

class Solution {
    Map<Integer, Integer> map;  // 方便根据数值查找位置

    // 根据中序遍历和后序遍历数组构建二叉树
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        map = new HashMap<>();  // 初始化map,用于保存中序遍历的数值与其索引的对应关系
        for (int i = 0; i < inorder.length; i++) {  // 遍历中序遍历数组
            map.put(inorder[i], i);  // 将每个值和它的索引存入map
        }

        // 调用findNode方法开始构建二叉树,初始范围为整个数组
        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;
    }
}

  • 9
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值