LeetCode每日十题---树(二)

1.题目描述

在这里插入图片描述

1.1笔者解答

class Solution {
    public boolean isSubPath(ListNode head, TreeNode root) {
        if (head == null)
            return true;
        if (root == null)
            return false;
       	//这样子如果满足条件,那么一直从根节点往下找,无法退回到以左右孩子为根节点寻找
        if (root.val == head.val)
            return isSubPath(head.next, root.left) || isSubPath(head.next, root.right);
        else
            return isSubPath(head, root.left) || isSubPath(head, root.right);
    }
}

1.2笔者分析

很简单的题目,但也很容易出错,上面的代码还有两个用例没有通过,其实整个代码都是有问题的。只有一个递归是错误的,因为如果从选择根结点往下找之后,无法退回到以左右孩子为根结点寻找。以下为正确解答

class Solution {
    public boolean isSubPath(ListNode head, TreeNode root) {
        if(root==null)return false;
        return isSubPathFromRoot(head,root)||isSubPath(head,root.left)||isSubPath(head,root.right);
    }
    private boolean isSubPathFromRoot(ListNode head,TreeNode root){
        if(head==null)return true;
        if(root==null)return false;
        if(root.val==head.val)
          return isSubPathFromRoot(head.next,root.left)||isSubPathFromRoot(head.next,root.right);
        return false;
    }
}

2.题目描述

在这里插入图片描述

2.1笔者分析

这题是需要往三个方向上寻找,而一个树结点只保存了左右子树的根结点,所以我们需要用一个数据结构来存储该结点第三个方向即父节点的位置,即下述代码的find方法,这也就侧面的将树结构改为了图结构,再用深度优先遍历即可,挺不错的想法。

class Solution {
    private Map<TreeNode,TreeNode> parents=new HashMap<>();
    private Set<TreeNode> used=new HashSet<>();
    private TreeNode targetNode;
    public List<Integer> distanceK(TreeNode root, TreeNode target, int K) {
        find(root,null,target);
        List<Integer> res=new LinkedList<>();
        dfs(targetNode,res,K);
        return res;
    }
    private void find(TreeNode root,TreeNode parent,TreeNode target){
        if(null==root){return ;}
        if(root.val==target.val){targetNode=root;}
        parents.put(root,parent);
        find(root.left,root,target);
        find(root.right,root,target);
    }
    private void dfs(TreeNode root,List<Integer> collector,int distance){
        if(root!=null&&!used.contains(root)){
            used.add(root);
            if(distance<=0){
                collector.add(root.val);
                return ;
            }
            dfs(root.left,collector,distance-1);
            dfs(root.right,collector,distance-1);
            dfs(parents.get(root),collector,distance-1);
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

赶路的苟狗

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

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

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

打赏作者

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

抵扣说明:

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

余额充值