863. All Nodes Distance K in Binary Tree

本文介绍了一种高效的算法,通过遍历和哈希映射找到从给定二叉树的根节点到目标节点的K步路径。首先将树结构转化为数组,然后利用O(n)复杂度的方法确定父节点和邻居节点,最后根据k值递归查找解决方案。适用于解决与二叉树路径相关的问题。
摘要由CSDN通过智能技术生成

Loading...

思路:先找到父,然后做类似爆炸的寻找,看成图去找

思路2还没写:1 用O(n)把树打成数组 ,转换过程中如果找到target 就记录数组下标 2 根据当前target  进行运算  找 父节点  左右叶子节点,带入k 当k为0 说明算出一个解 3 循环get解-也就是数组下标,不为空就是有解

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public List<Integer> distanceK(TreeNode root, TreeNode target, int k) {
         List<Integer> list = new ArrayList<>();
        //找到当前节点以及父节点
        Map<TreeNode, TreeNode> map =new HashMap<>();
        getFather(root, target,map);
        //根据父节点找相邻
        getFatherNbh( target, map, k,list);
        //根据当前节点的下节点找
        getNbh(target, k, 0,list);

        return list;
    }
    //找父亲距离k的
    private void getFatherNbh( TreeNode target, Map<TreeNode, TreeNode> map, int k,List<Integer> list) {
        boolean a = true;
        TreeNode nodeBox = target;
        //找父节点一直到没有为止
        while (a){
            TreeNode father = map.get(nodeBox);
            if (father != null) {
                  k--;
                if (k == 0) {
                    list.add(father.val);
                    return;
                }
                //判断是左节点还是右节点
                if (father.left != null) {
                    if (father.left.val != nodeBox.val) {
                        getNbh(father,k,1,list);
                    }
                }
                if (father.right != null) {
                    if (father.right.val != nodeBox.val) {
                        getNbh(father,k,2,list);
                    }
                }
                nodeBox = father;
            }else {
                a=false;
            }
        }
    }

    //找出当前节点下的 所有k步能到的
    private void getNbh(TreeNode target, int k, int i,List<Integer> list) {
        //i 0 代表说有  1 代表左边  2 代表右边
        if (target == null) {
           return;
        }
        if (k == 0) {
            list.add(target.val);
            return;
        }
        if (i == 0||i==1) {
            getNbh(target.left,k-1,0,list);
        }
        if (i == 0||i==2) {
            getNbh(target.right,k-1,0,list);
        }
    }

    /**
     * 找爸爸
     *
     * @param root
     * @param target
     * @return
     */
    private TreeNode getFather(TreeNode root, TreeNode target,Map<TreeNode, TreeNode> map) {

        if (root == null) {
            return null;
        }
        
       TreeNode l =  getFather(root.left,target,map);

        TreeNode r =   getFather(root.right,target,map);

        if (l != null) {
            map.put(l,root);
            return root;
        }
        if (r != null) {
            map.put(r,root);
            return root;
        }
        if (root.val == target.val) {
            return target;
        }
        return null;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值