257. Binary Tree Paths [Easy]

本文介绍了如何使用栈实现深度优先搜索(DFS)在二叉树中寻找所有可能的路径,同时展示了如何通过队列实现广度优先搜索(BFS)。展示了三种不同数据结构在LeetCode问题中的解决方案,包括递归、辅助函数递归以及使用Deque的两种算法。
摘要由CSDN通过智能技术生成

还有用stack做dfs、用queue做bfs的方法:

https://leetcode.com/problems/binary-tree-paths/discuss/68278/My-Java-solution-in-DFS-BFS-recursion 

/**
 * 自己的代码,recursive
 * Runtime: 8 ms, faster than 56.83%
 * Memory Usage: 38.6 MB, less than 99.07%
 */
class Solution {
    public List<String> binaryTreePaths(TreeNode root) {
        List<String> res = new ArrayList<>();
        if (root == null)
            return res;
        if (root.left == null && root.right == null) { // 叶子节点
            res.add(Integer.toString(root.val));
            return res;
        } 
        for (String s : binaryTreePaths(root.left)) // 给左子树的路径加上当前节点,并add进res
            res.add(root.val + "->" + s);
        for (String s : binaryTreePaths(root.right)) // 给右子树的路径加上当前节点,并add进res
            res.add(root.val + "->" + s);
        return res;
    }
}
/**
 * 用helper的recursive方法
 * Runtime: 1 ms, faster than 99.88%
 * Memory Usage: 38.8 MB, less than 96.40%
 */
class Solution {
    public List<String> binaryTreePaths(TreeNode root) {
        List<String> res = new ArrayList<>();
        helper(root, res, new StringBuilder());
        return res;
    }
    private void helper(TreeNode root, List<String> res, StringBuilder path) {
        if (root == null)
            return;
        int len = path.length(); // 记录修改前path的长度,用于还原path
        path.append(root.val);
        if (root.left == null && root.right == null) // 叶子节点
            res.add(path.toString());
        else {
            path.append("->");
            helper(root.left, res, path);
            helper(root.right, res, path);
        }
        path.setLength(len); // 还原path
    }
}
/**
 * DFS using Deque
 * Runtime: 9 ms, faster than 25.39%
 * Memory Usage: 39.1 MB, less than 57.05%
 */
class Solution {
    public List<String> binaryTreePaths(TreeNode root) {
        List<String> res = new ArrayList<>();
        if (root == null)
            return res;
        
        Deque<TreeNode> nodeD = new LinkedList<>(); // 存储DFS中treenode的stack
        Deque<String> strD = new LinkedList<>(); // 存储DFS中string的stack
        nodeD.push(root);
        strD.push("");
        
        while (!nodeD.isEmpty()) { // DFS
            TreeNode currNode = nodeD.pop();
            String currStr = strD.pop();
            if (currNode.left == null && currNode.right == null)
                res.add(currStr + currNode.val);
            String path = currStr + currNode.val + "->";
            if (currNode.right != null) {
                nodeD.push(currNode.right);
                strD.push(path);
            }
            if (currNode.left != null) {
                nodeD.push(currNode.left);
                strD.push(path);
            }
        }
        return res;
    }
}
/**
 * BFS using Deque
 * Runtime: 9 ms, faster than 25.39%
 * Memory Usage: 39.6 MB, less than 15.53%
 */
class Solution {
    public List<String> binaryTreePaths(TreeNode root) {
        List<String> res = new ArrayList<>();
        if (root == null)
            return res;
        
        Deque<TreeNode> nodeD = new LinkedList<>(); // 存储BFS中treenode的queue
        Deque<String> strD = new LinkedList<>(); // 存储BFS中string的queue
        nodeD.offer(root);
        strD.offer("");
        
        while (!nodeD.isEmpty()) { // BFS
            TreeNode currNode = nodeD.poll();
            String currStr = strD.poll();
            if (currNode.left == null && currNode.right == null)
                res.add(currStr + currNode.val);
            String path = currStr + currNode.val + "->";
            if (currNode.left != null) {
                nodeD.offer(currNode.left);
                strD.offer(path);
            }
            if (currNode.right != null) {
                nodeD.offer(currNode.right);
                strD.offer(path);
            }
        }
        return res;
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值