题目:

Given a binary tree, return all root-to-leaf paths.

For example, given the following binary tree:

   1
/ \
2 3
\
5

All root-to-leaf paths are:

["1->2->5", "1->3"]

链接: http://leetcode.com/problems/binary-tree-paths/

题解:

求Binary Tree所有路径。用Recursive解法会比较容易。逻辑是,假如为current root为leaf node,则在res中加入该节点,返回。否则,递归求解,在左子树和右子树每一个结果中,在String前面insert  root.val + "->"。

Time Complexity - O(n), Space Complexity - O(n)

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public 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(String.valueOf(root.val));
} else {
for(String s : binaryTreePaths(root.left)) {
res.add(String.valueOf(root.val) + "->" + s);
}
for(String s : binaryTreePaths(root.right)) {
res.add(String.valueOf(root.val) + "->" + s);
}
} return res;
}
}

二刷:

还是使用了最简单的DFS Recursive解法。更好的解法可能是stack DFS和queue BFS。

Java:

Time Complexity - O(n), Space Complexity - O(n)

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public 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(root.val + "");
return res;
}
List<String> left = binaryTreePaths(root.left);
List<String> right = binaryTreePaths(root.right);
for (String s : left) {
res.add(root.val + "->" + s);
}
for (String s : right) {
res.add(root.val + "->" + s);
}
return res;
}
}

三刷:

完全忘了一刷二刷是怎么写的,直接就用dfs + backtracking了。辅助方法里面用的是in-order traversal。 这里使用了一个List<Integer>来辅助计算Path,否则单独使用一个StringBuilder并不十分方便。

Java:

Time Complexity - O(n), Space Complexity - O(n)

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<String> binaryTreePaths(TreeNode root) {
List<String> res = new ArrayList<>();
List<Integer> path = new ArrayList<>();
getBinaryTreePaths(res, path, root);
return res;
} private void getBinaryTreePaths(List<String> res, List<Integer> path, TreeNode root) {
if (root == null) return;
path.add(root.val);
if (root.left == null && root.right == null) {
StringBuilder sb = new StringBuilder();
for (int val : path) sb.append(val).append("->");
sb.setLength(sb.length() - 2);
res.add(sb.toString());
path.remove(path.size() - 1);
return;
}
getBinaryTreePaths(res, path, root.left);
getBinaryTreePaths(res, path, root.right);
path.remove(path.size() - 1);
}
}

Update:

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<String> binaryTreePaths(TreeNode root) {
List<String> res = new ArrayList<>();
binaryTreePaths(res, new ArrayList<Integer>(), root);
return res;
} private void binaryTreePaths(List<String> res, List<Integer> path, TreeNode root) {
if (root == null) return;
path.add(root.val);
if (root.left == null && root.right == null) {
StringBuilder sb = new StringBuilder();
for (int val : path) sb.append(val).append("->");
sb.setLength(sb.length() - 2);
res.add(sb.toString());
path.remove(path.size() - 1);
return;
}
binaryTreePaths(res, path, root.left);
binaryTreePaths(res, path, root.right);
path.remove(path.size() - 1);
}
}

写在一个方法里,仿照一刷二刷的dfs:

这里每次递归都要创建新的ArrayList,并且左右子树都要计算,空间复杂度会比较高。怎么计算清楚,留给下一次了。

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public 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(String.valueOf(root.val)); List<String> left = binaryTreePaths(root.left);
for (String leftPath :left) res.add(root.val + "->" + leftPath); List<String> right = binaryTreePaths(root.right);
for (String rightPath :right) res.add(root.val + "->" + rightPath); return res;
}
}

Reference:

https://leetcode.com/discuss/55451/clean-solution-accepted-without-helper-recursive-function

https://leetcode.com/discuss/52072/accepted-java-simple-solution-in-8-lines

https://leetcode.com/discuss/52020/5-lines-recursive-python

https://leetcode.com/discuss/65362/my-concise-java-dfs-solution

https://leetcode.com/discuss/67749/bfs-with-two-queue-java-solution

257. Binary Tree Paths的更多相关文章

  1. &amp;lt;LeetCode OJ&amp;gt; 257. Binary Tree Paths

    257. Binary Tree Paths Total Accepted: 29282 Total Submissions: 113527 Difficulty: Easy Given a bina ...

  2. 【LeetCode】257. Binary Tree Paths

    Binary Tree Paths Given a binary tree, return all root-to-leaf paths. For example, given the followi ...

  3. Leetcode 257. Binary Tree Paths

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  4. (easy)LeetCode 257.Binary Tree Paths

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  5. Java [Leetcode 257]Binary Tree Paths

    题目描述: Given a binary tree, return all root-to-leaf paths. For example, given the following binary tr ...

  6. LeetCode OJ 257. Binary Tree Paths

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  7. LeetCode 257. Binary Tree Paths (二叉树路径)

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  8. 【一天一道LeetCode】#257. Binary Tree Paths

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  9. [LeetCode&amp;Python] Problem 257. Binary Tree Paths

    Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example ...

随机推荐

  1. java 编译时的初始化顺序

    有的时候,java的初始化会对我的工作照成很大影响,所以简单介绍一下, 首先介绍简单的变量的初始化:在类的内部,变量定义的先后顺序决定了初始化的顺序,即使变量定义散布于方法定义之间,它也会先于构造器和 ...

  2. Windows Azure移动终端云服务管理(公测版)

    概览 云在远方,管理在您手中.在这个移动为先 云为先的世界,服务不再是基于请求才提供,而是主动来到身边方便您的模式了.我们最近将会陆续推出几大移动端利器帮助您随时随地管理您的云服务. 首批利器之中排名 ...

  3. PHP静态化之真静态化

    参考文献:http://blog.sina.com.cn/s/blog_66aa1142010114lc.html 采用了动态服务器技术生成静态HTML的做法,这样做的好处是:一是能减轻其服务器的负担 ...

  4. javaEE的13种核心技术规范

    javaEE平台由一整套服务(Services).应用程序接口(APIs)和协议构成,它对开发基于Web的多层应用提供了功能支持,下面对javaEE中的13种技术规范进行简单的记录:   J2EE中的 ...

  5. Ubuntu 忘记密码

    1重启电脑Shift键进入GRUB引导模式如下图所示,选择第二行的recovery mode. 2 安e进入recovery mode 编译kernel进行启动参数 3 在linux /boot/vm ...

  6. [Swift]LeetCode517. 超级洗衣机 | Super Washing Machines

    You have n super washing machines on a line. Initially, each washing machine has some dresses or is ...

  7. NEO智能合约开发(一)不可能完成的任务

    悬赏任务 兹有如下合约 public static object Main(string method, object[] args) { if (Runtime.Trigger == Trigger ...

  8. 修改输入框placeholder的样式

    1. h5页面: //修改placeholder 样式 (chrome,其余类似加前缀) ::-webkit-input-placeholder { color:rgba(21,30,38,0.35) ...

  9. 在新安装的Centos中安装python3.7 解决pip和yum问题

    首先要先安装依赖包: yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-deve ...

  10. 几个常用linux命令语句

    几个常用linux命令 英文原文:What are the most useful "Swiss army knife" one-liners on Unix? 下面的这些内容是 ...