随想录刷题笔记 —二叉树篇6 513找树左下角的值 112路径总和 106中序后序构造二叉树

513找树左下角的值

找出该二叉树的 最底层 最左边节点的值

解法一:队列------层次遍历

java 复制代码
class Solution {
    public int findBottomLeftValue(TreeNode root) {
        Queue<TreeNode> treeNodes = new LinkedList<>();
        treeNodes.offer(root);
        int count = 1;
        int result = root.val;
        while (!treeNodes.isEmpty()){
            TreeNode temp = treeNodes.poll();
            count--;
            if (temp.left!=null){
                treeNodes.offer(temp.left);
            }
            if (temp.right!=null){
                treeNodes.offer(temp.right);
            }
            if (count==0){
                if (!treeNodes.isEmpty()){
                    result = treeNodes.peek().val;
                }
                count = treeNodes.size();
            }
        }
        return result;
    }
}

解法二:递归

设置maxdepth变量,记录最大深度。

java 复制代码
class Solution {
    public int findBottomLeftValue(TreeNode root) {
        fBLValue(root, 1);
        return resultval;
    }
    int maxdepth = 0;
    int resultval = 0;
    void fBLValue(TreeNode root, int depth) {
        if (root.right==null&&root.left==null){
            if (maxdepth<depth){
                resultval=root.val;
                maxdepth=depth;
            }
            return;
        }
        if (root.left!=null){
            fBLValue(root.left, depth+1);
        }
        if (root.right!=null){
            fBLValue(root.right, depth+1);
        }
    }
}

112路径总和

是否存在 根节点到叶子节点 的路径,这条路径上所有节点值相加等于目标和 targetSum

递归:传入参数value,表示从根节点到此节点的路径值。

当结点是叶子节点时,计算路径值是否=targetSum

java 复制代码
class Solution {
    public boolean hasPathSum(TreeNode root, int targetSum) {
        if (root==null){
            return false;
        }
        return hasPathS(root, targetSum, 0);
    }
    boolean hasPathS(TreeNode root, int targetSum, int value) {
        if (root==null){
            return false;
        }
        if (root.right==null&&root.left==null){
            return value + root.val == targetSum;
        }
        return hasPathS(root.left, targetSum, value+root.val)||hasPathS(root.right, targetSum, value+root.val);
        }
}

106中序后序构造二叉树

递归:以 后序 数组的最后一个元素为切割点,先切中序数组,根据中序数组,反过来再切后序数组。

一层一层切下去,每次后序数组最后一个元素就是节点元素

java 复制代码
class Solution {
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        TreeNode root = new TreeNode(postorder[postorder.length-1], null, null);
        int mid = findindex(inorder, postorder[postorder.length-1], 0, inorder.length-1);
        if (mid!=0){
            root.left = buildTree(Arrays.copyOfRange(inorder, 0, mid), Arrays.copyOfRange(postorder, 0, mid));
        }
        if (mid!=inorder.length-1){
            root.right = buildTree(Arrays.copyOfRange(inorder, mid+1, inorder.length), Arrays.copyOfRange(postorder, mid, postorder.length-1));
        }
        return root;
    }

    int findindex(int[] order, int value, int left, int right){
        for (int i = left; i <= right; i++) {
            if (value==order[i]){
                return i;
            }
        }
        return -1;
    }
}

收获

二叉树就是统统递归,回溯

相关推荐
茂桑4 分钟前
MVCC(多版本并发控制)
java·开发语言·数据库
customer0819 分钟前
【开源免费】基于SpringBoot+Vue.JS医疗报销系统(JAVA毕业设计)
java·vue.js·spring boot·后端·开源
barcke28 分钟前
【深度解析】Java接入DeepSeek大模型:从零实现流式对话+多轮会话管理(完整项目实战) —— SpringBoot整合、API安全封装、性能优化全攻略
java·spring boot
愈谦卑33 分钟前
数据结构:排序
数据结构·算法·排序算法
zl97989940 分钟前
MybatisPlus-注解
java·spring·maven
好记性+烂笔头41 分钟前
hot100_108. 将有序数组转换为二叉搜索树
算法·leetcode·职场和发展
杰九1 小时前
【环境配置】maven,mysql,node.js,vue的快速配置与上手
java·vue.js·spring boot·mysql·node.js·maven
tt5555555555551 小时前
每日一题——主持人调度(二)
c语言·数据结构·算法·leetcode·八股文
wapicn991 小时前
‌挖数据平台对接DeepSeek推出一键云端部署功能:API接口驱动金融、汽车等行业智能化升级
java·人工智能·python·金融·汽车·php
技术蔡蔡1 小时前
Android字节码处理-函数耗时统计揭秘
算法·面试