[Leetcode][第257题][JAVA][二叉树的所有路径][BFS][DFS]

239 篇文章 1 订阅
【问题描述】[简单]

在这里插入图片描述

【解答思路】
1. DFS

在这里插入图片描述

时间复杂度:O(N^2) 空间复杂度:O(N^2)
在这里插入图片描述

class Solution {
    public List<String> binaryTreePaths(TreeNode root) {
        List<String> paths = new ArrayList<String>();
        constructPaths(root, "", paths);
        return paths;
    }

    public void constructPaths(TreeNode root, String path, List<String> paths) {
        if (root != null) {
            StringBuffer pathSB = new StringBuffer(path);
            pathSB.append(Integer.toString(root.val));
            if (root.left == null && root.right == null) {  // 当前节点是叶子节点
                paths.add(pathSB.toString());  // 把路径加入到答案中
            } else {
                pathSB.append("->");  // 当前节点不是叶子节点,继续递归遍历
                constructPaths(root.left, pathSB.toString(), paths);
                constructPaths(root.right, pathSB.toString(), paths);
            }
        }
    }
}

时间复杂度更高 ,使用String+“ ”拼接字符串

class Solution {

    //这行代码可不敢写这里,leetcode提交会出问题的。。。。!!!
    //private static List<String> res = new ArrayList<String>();

    public List<String> binaryTreePaths(TreeNode root) {

        List<String> res = new ArrayList<String>();
        if(root == null)return res;
        BL(root,res,"");

        return res;
    }

    public static void BL(TreeNode node,List<String> res,String str){

        if(node.left != null) BL(node.left, res, str + node.val + "->");

        if(node.right != null) BL(node.right, res, str + node.val + "->");
        
        if(node.left == null && node.right == null) res.add(str + node.val);
        
    }
}


链接:https://leetcode-cn.com/problems/binary-tree-paths/solution/chang-gui-cao-zuo-50di-gui-by-gu-xiong-007/

2. BFS

在这里插入图片描述

时间复杂度:O(N2) 空间复杂度:O(N2)
在这里插入图片描述

class Solution {
    public List<String> binaryTreePaths(TreeNode root) {
        List<String> paths = new ArrayList<String>();
        if (root == null) {
            return paths;
        }
        Queue<TreeNode> nodeQueue = new LinkedList<TreeNode>();
        Queue<String> pathQueue = new LinkedList<String>();

        nodeQueue.offer(root);
        pathQueue.offer(Integer.toString(root.val));

        while (!nodeQueue.isEmpty()) {
            TreeNode node = nodeQueue.poll(); 
            String path = pathQueue.poll();

            if (node.left == null && node.right == null) {
                paths.add(path);
            } else {
                if (node.left != null) {
                    nodeQueue.offer(node.left);
                    pathQueue.offer(new StringBuffer(path).append("->").append(node.left.val).toString());
                }

                if (node.right != null) {
                    nodeQueue.offer(node.right);
                    pathQueue.offer(new StringBuffer(path).append("->").append(node.right.val).toString());
                }
            }
        }
        return paths;
    }
}

【总结】
1. String StringBuffer StringBuilder 在这里插入图片描述
2.二叉树遍历
  • 前序遍历 先输出当前结点的数据,再依次遍历输出左结点和右结点
  • 中序遍历 先遍历输出左结点,再输出当前结点的数据,再遍历输出右结点
  • 后续遍历 先遍历输出左结点,再遍历输出右结点,最后输出当前结点的数据
3.复制粘贴时候要小心 修改相应代码 脑子模拟代码 快速排查错误 多从自己写的代码 切忌浮躁

转载链接:https://leetcode-cn.com/problems/binary-tree-paths/solution/er-cha-shu-de-suo-you-lu-jing-by-leetcode-solution/
参考链接:https://leetcode-cn.com/problems/binary-tree-paths/solution/chang-gui-cao-zuo-50di-gui-by-gu-xiong-007/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值