LeetCode——Binary Tree Paths
Description:
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"]
/**
* 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> paths;
public List<Integer> path;
public List<String> binaryTreePaths(TreeNode root) {
paths = new ArrayList<String>();
path = new ArrayList<Integer>();
getAllPath(root); return paths;
} public void getAllPath(TreeNode node) { // 1
// / \
// 2 3 ["1->2->5", "1->3"]
// \
// if(node == null) {
return ;
}
path.add(node.val);
if(node.left==null && node.right==null) {
StringBuilder onePath = new StringBuilder();
for(int i=0; i<path.size(); i++) {
if(i != 0) onePath.append("->");
onePath.append(path.get(i));
}
paths.add(onePath.toString());
}
getAllPath(node.left);
getAllPath(node.right);
path.remove(path.size() - 1); }
}
LeetCode——Binary Tree Paths的更多相关文章
- [LeetCode] Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- leetcode : Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- Python3解leetcode Binary Tree Paths
问题描述: Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. E ...
- LeetCode Binary Tree Paths(简单题)
题意: 给出一个二叉树,输出根到所有叶子节点的路径. 思路: 直接DFS一次,只需要判断是否到达了叶子,是就收集答案. /** * Definition for a binary tree node. ...
- leetcode Binary Tree Paths python
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...
- 【LeetCode】257. Binary Tree Paths
Binary Tree Paths Given a binary tree, return all root-to-leaf paths. For example, given the followi ...
- <LeetCode OJ> 257. Binary Tree Paths
257. Binary Tree Paths Total Accepted: 29282 Total Submissions: 113527 Difficulty: Easy Given a bina ...
- [LintCode] Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths.Example Given the following binary tree: 1 / \2 ...
- LeetCode_257. Binary Tree Paths
257. Binary Tree Paths Easy Given a binary tree, return all root-to-leaf paths. Note: A leaf is a no ...
随机推荐
- Linux 网络子系统之网络协议接口层(二)
这一篇主要围绕网络协议接口层的发送函数的解析 int dev_queue_xmit(struct sk_buff *skb) 函数解析 声明: /* include/linux/netdevice.h ...
- linux异步通知
简述 linux下异步方式有两种:异步通知和异步IO(AIO),aio请参考:linux异步IO--aio 异步通知的含义是:一旦设备就绪,则主动通知应用程序,这样应用程序就不需要查询设备状态,准确称 ...
- PixelMatorPro快捷键大全(osx)
Keyboard Shortcuts Navigate an image Zoom in Command-Plus (+) Zoom out Command-Minus (-) Zoom to f ...
- IXWebHosting主机如何退款中文图解教程
http://www.chinaz.com/web/2011/0630/192654.shtml ——————————————————————————————————————————————————— ...
- SpringMVC & SpringBoot小记
SpringMVC 1.SpringMVC常用注解 https://blog.csdn.net/lipinganq/article/details/79155072 :@Component.@Serv ...
- -27979 LoadRunner 错误27979 找不到请求表单 Action.c(73): Error -27979: Requested form not found
LoadRunner请求无法找到:在录制Web协议脚本回放脚本的过程中,会出现请求无法找到的现象,而导致脚本运行停止. 错误现象:Action.c(41): Error -27979: Request ...
- ASP.NET WebApi 路由配置【转】
一.路由介绍 ASP.NET Web API路由是整个API的入口.我们访问某个资源就是通过路由映射找到对应资源的URL.通过URL来获取资源的. 对于ASP.NET Web API内部实现来讲,我们 ...
- js 动态设置 option 的selected 选项
思路:通过for循环判断每个选项,一旦满足条件则设置其selected属性为true即可,关键代码: var obj = document.getElementById(select_id); for ...
- Linux 权限修改
chown -R 用户名:组名 文件夹名chown -R xu:hadoop hadoop 将hadoop目录(以及其下的所有子目录/文件)的属主用户设为xu, xu 的组名为group
- oracle 死锁和锁等待的区别
所谓的锁等待:就是一个事务a对一个数据表进行ddl或是dml操作时,系统就会对该表加上表级的排它锁,此时其他的事务对该表进行操作的时候会等待a提交或是回滚后,才可以继续b的操作 所谓的死锁:当两个或多 ...