随想录刷题笔记 —二叉树篇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;
    }
}

收获

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

相关推荐
CoovallyAIHub3 分钟前
突破360°跟踪极限!OmniTrack++:全景MOT新范式,HOTA指标狂飙43%
深度学习·算法·计算机视觉
JavaGuide5 分钟前
OPPO 后端校招面试,过于简单了!
java·后端
码割机10 分钟前
Linux服务器安装jdk和maven详解
java·linux·maven
得物技术31 分钟前
得物管理类目配置线上化:从业务痛点到技术实现
后端·算法·数据分析
go_bai35 分钟前
Linux--进程池
linux·c++·经验分享·笔记·学习方法
QT 小鲜肉38 分钟前
【QT/C++】Qt网络编程进阶:UDP通信和HTTP请求的基本原理和实际应用(超详细)
c语言·网络·c++·笔记·qt·http·udp
轻舟客丶1 小时前
ORA-03113的解决方案
数据库·经验分享·笔记·oracle
老虎06271 小时前
黑马点评学习笔记07(缓存工具封装)
笔记·学习·缓存
青木川崎1 小时前
linux面试题
java·linux·运维
CoovallyAIHub1 小时前
首个大规模、跨模态医学影像编辑数据集,Med-Banana-50K数据集专为医学AI打造(附数据集地址)
深度学习·算法·计算机视觉