第270场周赛灵动科技.T3从二叉树一个节点到另一个节点每一步的方向

解题思路

将二叉树转为图,并规定1代表父子关系,2代表左子节点,3代表右子节点
总的来说很巧妙,但是需要优化,否则会超时

代码

/**
 * @author bakazhou
 * @description
 * @date 2021年12月07日 14:06
 **/
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 String getDirections(TreeNode root, int startValue, int destValue) {
        if (root == null){
            return "";
        }
        //求出二叉树的节点数量
        int size = 0;
        LinkedList<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()){
            int n = queue.size();
            size += n;
            for (int i=0;i<n;i++){
                TreeNode first = queue.getFirst();
                queue.removeFirst();
                if (first.left!=null){
                    queue.add(first.left);
                }
                if (first.right!=null){
                    queue.add(first.right);
                }
            }
        }
        //转变为图的方式求解
        //规定0代表非连通 1代表为父子关系 2代表左子节点 3代表右子节点
        int[][] graph = new int[size+1][size+1];
        queue.add(root);
        while (!queue.isEmpty()){
            int n = queue.size();
            size += n;
            for (int i=0;i<n;i++){
                TreeNode first = queue.getFirst();
                queue.removeFirst();
                if (first.left!=null){
                    queue.add(first.left);
                    graph[first.val][first.left.val] = 2;
                    graph[first.left.val][first.val] = 1;
                }
                if (first.right!=null){
                    queue.add(first.right);
                    graph[first.val][first.right.val] = 3;
                    graph[first.right.val][first.val] = 1;
                }
            }
        }
        //用于记录路径
        LinkedList<String> road = new LinkedList<>();
        //用于记录当前节点数字
        LinkedList<Integer> num = new LinkedList<>();
        Set<Integer> set = new HashSet<>();
        road.add("");
        num.add(startValue);
        set.add(startValue);
        while (!road.isEmpty()){
            int n = road.size();
            for (int i=0;i<n;i++){
                int first = num.getFirst();
                num.removeFirst();
                String roadFirst = road.getFirst();
                road.removeFirst();
                if (first == destValue){
                    return roadFirst;
                }
                int[] go = graph[first];
                for (int j=0;j<go.length;j++){
                    StringBuilder sb = new StringBuilder();
                    sb.append(roadFirst);
                    if (go[j] == 1 || go[j] == 2 || go[j] == 3 && !set.contains(j)){
                        set.add(j);
                        num.add(j);
                        if (go[j] == 1){
                            sb.append('U');
                        }
                        else if (go[j] == 2){
                            sb.append('L');
                        }
                        else if (go[j] == 3){
                            sb.append('R');
                        }
                        road.add(sb.toString());
                    }
                }
            }
        }
        return "";
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值