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 ...
随机推荐
- sendmsg/recvmsg和struct msghdr
函数原型 #include <sys/types.h> #include <sys/socket.h> ssize_t sendmsg(int sockfd, const st ...
- EMC检测标准
- 启动hive出错,提示没有权限
报错信息如下: which: no hbase in (/usr/local/sqoop-1.4.6.bin__hadoop-2.0.4-alpha/bin:/usr/local/hive/bin:/ ...
- C# cs文件表头模版
设置位置:C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\ItemTemplatesCache\CSharp\Web\2 ...
- 用户数据验证的正确姿势之assert
用户数据验证灰常重要, 不用多说了, 但是实现方法(准确的说是表现形式)有很多人, 如何优雅的完成一个后端验证过滤器是一个值得考量的问题, 我尝试过许多方法, 比如validator.js模块, ex ...
- java.lang.UnsatisfiedLinkError: No implementation found for int com.xxx.xx中的couldn’t find “XX.so”或loadLibrary("xxx")失败
我觉得这是个神坑,虽然早几年网上就很多po出来的解决方式,但是同样的问题,我的bug却稳如泰山,一点用都没有,好气 下面总结一下 这里前面先是有个系统打印信息 I/System.out: loadLi ...
- 【转】WCF入门教程二[WCF应用的通信过程]
一.概述 WCF能够建立一个跨平台的安全.可信赖.事务性的解决方案,是一个WebService,.Net Remoting,Enterprise Service,WSE,MSMQ的并集,有一副很经典的 ...
- Blend for Visual Studio 2013
软件开发中为了使设计师和程序员“并行”工作并直接参与到程序的开发中来. 1.在网络程序开发团队中,草图设计后,设计师们可以使用HTML.CSS.JavaScript直接生成UI,程序员则在这个UI产生 ...
- js unicode处理
//岗位详细界面 var str="1.\u000D\u21B52.\u000D\u21B53"; var pad = function() { var tbl = []; ret ...
- 解决myeclipse4.1.1对一个表生成映射文件的时候,出现“generating artifacts"的解决!
很多人在用myeclipse4.1.1对一个表生成映射文件的时候,都出现“generating artifacts"的问题.我也遇到了这个问题,弄得我也很郁闷!看了很多人的帖子后还是无法搞定 ...