LeetCode OJ 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"]
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
这个题目的难点在与分裂路径,我们在遍历到一个节点的时候需要把到达它的路径告诉它,如果这个节点是叶子节点,则把它加入路径然后添加到路径集合中。如果它不是叶子节点,则把它添加到路径中,并且继续遍历它的子节点。代码如下:
/**
* 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> list = new ArrayList(); public List<String> binaryTreePaths(TreeNode root) {
if(root == null) return list;
String s = "";
path(root, s);
return list;
} public void path(TreeNode root, String p){
String s = new String(p);
if(root.left==null && root.right==null){//如果是叶子节点
s = s + root.val;
list.add(s);
}
else s = s + root.val + "->";
if(root.left!=null) //如果左子树非空
path(root.left, s);
if(root.right!=null) //如果右子树非空
path(root.right, s);
}
}
LeetCode OJ 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&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 ...
- 【leetcode❤python】 257. Binary Tree Paths
深度优先搜索 # Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# se ...
- 【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 ...
- 【一天一道LeetCode】#257. Binary Tree Paths
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- [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 解题报告(java & python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leet ...
- Leetcode 257. Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
随机推荐
- selenium自动化遇见Cannot find class in classpath问题
今天遇见了Cannot find class in classpath的问题, org.testng.TestNGException: Cannot find class in classpath: ...
- Oracle获取时间日期月份星期数
1.日期和字符转换函数用法(to_date,to_char)select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') as nowTime from dual; ...
- cuckoo相关
Q1:pefile is out of date 现象:ERROR: Your version of pefile is out of date. Please update to the late ...
- 使用hexo搭建github博客
Win7系统已经安装了node.js和npm npm install -g hexo-cli 全局安装hexo客户端 hexo init blog 在喜欢的位置初始化blog目录 cd blog np ...
- 辽宁OI2016夏令营模拟T2-road
最短路(road.pas/c/cpp)题目大意有一个点数为 n,边数为 m 的无向图,点的编号为 1 到 n.边的权值均为非负数.现在请你求出从点 1 到点 n 的最短路径条数,若有无限条则输出-1, ...
- html 超链接(a)详细讲解
a:link : http://www.cnblogs.com/yangfeng/archive/2009/07/25/1530962.html 超级链接 超级链接是网站中使用比较频繁的HTML元素, ...
- Spring Security(07)——缓存UserDetails
Spring Security提供了一个实现了可以缓存UserDetails的UserDetailsService实现类,CachingUserDetailsService.该类的构造接收一个用于真正 ...
- 《Intel汇编第5版》 Intel CPU小端序
一.MASM汇编器中的数据类型 二.Intel汇编中的立即数类型 三.定义有符号和无符号整数 四.小端序 内存中数据按照字节存储,一个4个字节无符号整数,其高位存储在低地址上,低位存储在高地址上. 比 ...
- 7 个 jQuery 最佳实践
前言 随着富网络应用(rich web applications)数量的增长,以及用户对快速交互响应的高期望,开发者开始使用JavaScript库来快速高效的完成一些重复性的工作.这其中最流行的Jav ...
- ActiveMQ in Action(1) - JMS
关键字: activemq 1 JMS 在介绍ActiveMQ之前,首先简要介绍一下JMS规范.1.1 JMS的基本构件1.1.1 连接工厂 连接工厂是客户用来创建连接的对象,例如Acti ...