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 ...
随机推荐
- 启动hive出错,提示没有权限
报错信息如下: which: no hbase in (/usr/local/sqoop-1.4.6.bin__hadoop-2.0.4-alpha/bin:/usr/local/hive/bin:/ ...
- 对设计领域中Tile和Card的理解
前端工程师离不开设计, 谈到设计就要想到大名鼎鼎的material design主题, 而material是以card为经典单元的, card即卡片, 是层次化模型的最小模块, 用于提供扁平化的信息, ...
- LINQ操作符二:SelectMany
SelectMany操作符提供了将多个from子句组合起来的功能,相当于数据库中的多表连接查询,它将每个对象的结果合并成单个序列. 示例: student类: using System; using ...
- 在传统以太网中,为什么要有最小帧长度(64 bytes)和最大帧长度(1500 bytes)的限制?
遇到的问题:以太网的数据帧封装如下图所示,包含在IP数据报中的数据部分最长应该是( )字节? A.1434 B.1460 C.1480 D.1500 答案:C 原因: 以太网(IEEE 802.3)帧 ...
- Java中Calendar.DAY_OF_WEEK需要减一的原因
http://blog.sina.com.cn/s/blog_45c06e600100pm77.html ——————————————————————————————————————————————— ...
- mysql编译安装后各种常见错误集锦
1.ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', sys ...
- 将sqlServer上的数据库文件进行盘目的迁移
在数据库客户端创建数据库时要改为.mdf文件,因为附加问价时附加的是.mdf文件: 在里选中相应的数据库 右键->任务-分离 在 剪切到相应的想放置的盘目. 例如迁移到E盘下: 在数据库-> ...
- 关于Cocos2d-x中MoveTo等动作位置坐标和setPosition的位置坐标的区别
setPosition设置的坐标使用的是锚点的位置,会根据锚点的改变而有所不同 而MoveTo等动作位置坐标使用的是物体中心的位置,不受锚点的影响
- 最小顶点覆盖(Minimum Vertex Cover)与最大独立集(Maximum Independent Set)
问题描述:就是在图中找最小的点集,使得覆盖所有边. 和独立集等价:独立集问题:在图中找最大的点集,使得点集内的所有点互不相连. 引理:顶点覆盖集和独立集互补. 上面这个引理使得这两个问题可以相互规约, ...
- TensorFlow基础笔记(9) Tensorboard可视化显示以及查看pb meta模型文件的方法
参考: http://blog.csdn.net/l18930738887/article/details/55000008 http://www.jianshu.com/p/19bb60b52dad ...