day18- 513.找树左下角的值 | 112. 路径总和 | 106.从中序与后序遍历序列构造二叉树

513.找树左下角的值

题目链接:https://leetcode.cn/problems/find-bottom-left-tree-value/submissions/

题目要求:

🤔解题思路

递归+回溯

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    int depth = 0;    //存储最大深度
    int res = 0;    //存储结果
    public int findBottomLeftValue(TreeNode root) {
        get(root,1);    //根节点的深度为1
        return res;
    }
    public void get(TreeNode node,int h){
        //结束递归的条件,遍历到叶子节点
        if(node.left == null && node.right == null){
            //如果当前叶子节点大于最大深度,则更新结果
            //因为是先遍历左节点后遍历右节点,如果已经更新了左节点,则不会更新同一深度的右节点
            if(h > depth){
                depth = h;
                res = node.val;
            }
        }
        if(node.left != null){
            h++;
            get(node.left,h);
            h--;    //回溯
        }
        if(node.right != null){
            h++;
            get(node.right,h);
            h--;    //回溯
        }
    }
}

112. 路径总和

题目链接:https://leetcode.cn/problems/path-sum/submissions/

题目要求:

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

叶子节点 是指没有子节点的节点。

 

🤔解题思路

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public boolean hasPathSum(TreeNode root, int targetSum) {
        if(root == null) return false;
        targetSum -= root.val;
        if(root.left == null && root.right == null){
            if(targetSum == 0) return true;
            else return false;
        }
        if(root.left != null) if(hasPathSum(root.left,targetSum)) return true;
        if(root.right != null) if(hasPathSum(root.right,targetSum)) return true;
        return false;
    }
}

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

题目链接:https://leetcode.cn/problems/construct-binary-tree-from-inorder-and-postorder-traversal/submissions/

题目要求:

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

🤔解题思路

代码比较冗余但是比较好理解!根据后序遍历(左右中)数组的最后一个元素确定根节点在中序遍历(左中右)数组的索引,从而分割左右子树;再根据中序遍历分割的左右长度来分割后序遍历的左右子树

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        //退出递归的条件,数组为空
        if (postorder.length == 0) return null;
        int val = postorder[postorder.length - 1];
        //只有一个元素时,直接返回该节点
        TreeNode root = new TreeNode(val);
        if(inorder.length == 1) return root;
        //获取根节点在中序中的索引
        int index;
        for (index = 0; index < inorder.length - 1; index++) {
            if (inorder[index] == val) {
                break;
            }
        }
        //切割中序数组,构造左子树的中序数组
        int[] inleft = new int[index];
        for (int i = 0; i < index; i++) {
            inleft[i] = inorder[i];
        }
        //构造右子树的中序数组
        int size = inorder.length - index - 1;
        int[] inright = new int[size];
        int count = 0;
        for (int i = index + 1; i < inorder.length; i++) {
            inright[count++] = inorder[i];
        }
        //构造左子树的后序数组
        int[] postleft = new int[index];
        for (int i = 0; i < index; i++) {
            postleft[i] = postorder[i];
        }
        //构造右子树的后序数组
        int[] postright = new int[size];
        count = 0;
        for (int i = index; i < inorder.length - 1; i++) {
            postright[count++] = postorder[i];
        }
        root.left = buildTree(inleft, postleft);
        root.right = buildTree(inright, postright);
        return root;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值