257. Binary Tree Paths
题目:
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的更多相关文章
- <LeetCode OJ> 257. Binary Tree Paths
257. Binary Tree Paths Total Accepted: 29282 Total Submissions: 113527 Difficulty: Easy Given a bina ...
- 【LeetCode】257. Binary Tree Paths
Binary Tree Paths Given a binary tree, return all root-to-leaf paths. For example, given the followi ...
- Leetcode 257. Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- (easy)LeetCode 257.Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- Java [Leetcode 257]Binary Tree Paths
题目描述: Given a binary tree, return all root-to-leaf paths. For example, given the following binary tr ...
- LeetCode OJ 257. Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- LeetCode 257. Binary Tree Paths (二叉树路径)
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- 【一天一道LeetCode】#257. Binary Tree Paths
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- [LeetCode&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 ...
随机推荐
- mslookup
Microsoft Windows [版本 6.1.7601]版权所有 (c) 2009 Microsoft Corporation.保留所有权利. C:\Users\Administrator> ...
- 【C#】线程池
将方法排入队列以便执行,并指定包含该方法所用数据的对象.此方法在有线程池线程变得可用时执行. class Program { static void Main(string[] args) { str ...
- 【Jetlang】一个高性能的Java线程库
actor Jetlang 提供了一个高性能的Java线程库,该库是 JDK 1.5 中的 java.util.concurrent 包的补充,可用于基于并发消息机制的应用. .net的MS CCR ...
- WebAPi性能
提高WebAPi性能 前言 WebAPi作为接口请求的一种服务,当我们请求该服务时我们目标是需要快速获取该服务的数据响应,这种情况在大型项目中尤为常见,此时迫切需要提高WebAPi的响应机制,当然 ...
- JPA学习---第五节:日期和枚举等字段类型的JPA映射
1.在上一节可在数据库中看到创建出来的表和字段,是通过 Entity bean 来创建的,而创建表名和字段名的规则是怎样的? 有类,代码如下: package learn.jpa.bean; impo ...
- spicy及remote-viewer登录方法
spicy登录: $sudo spicy remote-viewer登录: $ sudo /usr/local/bin/remote-viewer $ spice://192.168.70.158:4 ...
- OpenWrt编译到底脚本
在办公室编译OpenWrt,费时很久,原因有两个. 一是办公室网络环境比较糟糕,经常断线不说,很多技术网站间歇性的连不上,不是撞到404就是DNS解析失败等. 二是初次编译OpenWrt时需要从网上下 ...
- C#笔记2:重构
转: 最常用的重构指导 参考:http://www.cnblogs.com/KnightsWarrior/archive/2010/06/30/1767981.html,本文示例代码多来自此处: 参考 ...
- .net framework 源码调试 与 问题解决
调试方式有二种, 看官方资料就OK. 官方地址: http://referencesource.microsoft.com/serversetup.aspx 1. 使用配置在线地址安装 2. 下载安装 ...
- linux下mysql的root密码忘记解决方
1.首先确认服务器出于安全的状态,也就是没有人能够任意地连接MySQL数据库. 因为在重新设置MySQL的root密码的期间,MySQL数据库完全出于没有密码保护的 状态下,其他的用户也可以任意地登录 ...