代码随想录算法训练营DAY18 | 二叉树 (5)

一、LeetCode 513 找树左下角的值

题目链接:513.找树左下角的值icon-default.png?t=N7T8https://leetcode.cn/problems/find-bottom-left-tree-value/

思路一:递归+回溯+全局变量比深度。

class Solution {
    int Max_depth = 0;
    int result = 0;
    public int findBottomLeftValue(TreeNode root) {
        travel(root,0);
        return result;
    }
    public void travel(TreeNode root, int depth){
        if(root.left == null && root.right == null){
            if(depth > Max_depth){
                Max_depth = depth;
                result = root.val;
            }
        }
        if(root.left != null){
            depth++;
            travel(root.left,depth);
            //回溯
            depth--;
        }
        if(root.right != null){
            depth++;
            travel(root.right,depth);
            depth--;
        }
        return;
    }
}
/**
 * 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 int findBottomLeftValue(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        int ans = 0;
        while(!queue.isEmpty()){
            int size = queue.size();
            for(int i = 0; i < size; i++){
                TreeNode node = queue.poll();
                //记录最后一行第一个值,即为树左下角的值
                if(i == 0){
                    ans = node.val;
                }
                if(node.left != null){
                    queue.offer(node.left);
                }
                if(node.right != null){
                    queue.offer(node.right);
                }
            }
        }
        return ans;
    }
}

 二、LeetCode 112 路径总和

题目链接:112.路径总和icon-default.png?t=N7T8https://leetcode.cn/problems/path-sum/

思路:前序遍历 + 叶子结点判断~

class Solution {
    public boolean hasPathSum(TreeNode root, int targetSum) {
        int sum = 0;
        return flag(root,targetSum,sum);
    }
    public boolean flag(TreeNode root, int target, int sum){
        if(root == null){
            return false;
        }
        //中 左 右
        sum += root.val;
        if(root.left == null && root.right == null){
            if(sum == target){
                return true;
            }
        }
        boolean left = flag(root.left, target, sum);
        boolean right = flag(root.right, target, sum);
        return left || right;
    }
}
/**
 * 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;
 *     }
 * }
 */

 三、LeetCode 106 从中序与后序遍历序列构造二叉树

题目链接:106.从中序与后序遍历序列构造二叉树icon-default.png?t=N7T8https://leetcode.cn/problems/construct-binary-tree-from-inorder-and-postorder-traversal/description/

思路:左闭右开,分割左右子树。

class Solution {
    Map<Integer,Integer> map = new HashMap<>();
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        for(int i = 0; i < inorder.length; i++){
            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;
    }
}
/**
 * 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;
 *     }
 * }
 */

 四、小结

        昨天有点事情,今日补上~

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

橙南花已开

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值