力扣labuladong——一刷day49

文章讲述了在二叉树结构中进行左下角值查找、路径总和计算、受污染二叉树元素查找以及判断子树等问题的解决方案,涉及递归遍历算法的应用。

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言


二叉树的递归分为「遍历」和「分解问题」两种思维模式,这道题需要用到「遍历」的思维模式。

一、力扣513. 找树左下角的值

/**
 * 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 len = -1;
    int res = 0;
    public int findBottomLeftValue(TreeNode root) {
        fun(root,1);
        return res;
    }
    public void fun(TreeNode root, int depth){
        if(root == null){
            return ;
        }
        if(depth > len){
            len = depth;
            res = root.val;
        }
        fun(root.left, depth+1);
        fun(root.right, depth+1);
    }
}

二、力扣666. 路径总和 IV

class Solution {
    int res = 0,path = 0;
    Map<Integer,Integer> map = new HashMap<>();
    public int pathSum(int[] nums) {
        for(int a : nums){
            int value = a%10;
            int pre = a/10;
            map.put(pre,value);
        }
        fun(1,1);
        return res;
    }
    public int[] decode(int pre){
        return new int[]{pre/10,pre%10};
    }
    public int encode(int row, int index){
        return row * 10 + index;
    }
    public void fun(int row, int index){
        int pre = encode(row,index);
        if(!map.containsKey(pre)){
            return;
        }
        path += map.get(pre);
        int left = encode(row+1,index*2-1);
        int right = encode(row+1,index*2);
        if(!map.containsKey(left) && !map.containsKey(right)){
            res += path;
            path -= map.get(pre);
            return;
        }
        fun(row+1,index*2-1);
        fun(row+1,index*2);
        path -= map.get(pre);
    }
}

三、力扣1261. 在受污染的二叉树中查找元素

/**
 * 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 FindElements {
    Map<Integer,Integer> map = new HashMap<>();
    TreeNode root = null;

    public FindElements(TreeNode root) {
        fun(root,0);
        this.root = root;
    }
    
    public boolean find(int target) {
        return map.containsKey(target);
    }
    public void fun(TreeNode root, int value){
        if(root == null){
            return ;
        }
        map.put(value,1);
        root.val = value;
        fun(root.left, value*2+1);
        fun(root.right, value*2+2);
    }
}

/**
 * Your FindElements object will be instantiated and called as such:
 * FindElements obj = new FindElements(root);
 * boolean param_1 = obj.find(target);
 */

四、力扣572. 另一棵树的子树

/**
 * 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 isSubtree(TreeNode root, TreeNode subRoot) {
        if(root == null){
            return subRoot == null;
        }
        if(fun(root,subRoot)){
            return true;
        }
        return isSubtree(root.left,subRoot) || isSubtree(root.right,subRoot);
    }
    public boolean fun(TreeNode root, TreeNode subRoot){
        if(root == null && subRoot == null){
            return true;
        }
        if(root == null || subRoot == null){
            return false;
        }
        if(root.val != subRoot.val){
            return false;
        }
        return fun(root.left,subRoot.left) && fun(root.right,subRoot.right);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

乱世在摸鱼

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

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

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

打赏作者

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

抵扣说明:

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

余额充值