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 ...
随机推荐
- python远程批量执行命令
#!/usr/bin/env python#-*- coding:utf-8 -*- from multiprocessing import Process,Poolimport time,param ...
- RF环境搭建
官网:http://robotframework.org/ 序号 安装包名 安装方法 下载地址 备注 1 python exe文件,直接双击安装 https://www.python.org/down ...
- ubuntu 搭建python2.x 抓取环境
1.apt-get install python-bs4 bs4只有py2的代码,安装在py3下会很麻烦 bs4支持HTML parser,也可以支持第三方的分析器 2.apt-get install ...
- Xcode-之Alcatraz
一.说明: Alcatraz 是一款 Xcode的插件管理工具,可以用来管理XCode的 插件.模版以及颜色配置的工具. 二.安装 1.github地址:https://github.com/alca ...
- CodeForces 696C PLEASE
快速幂,费马小定理,逆元. 设$dp[n]$表示$n$次操作之后的概率,那么$dp[n] = \frac{{(1 - dp[n - 1])}}{2}$.$1-dp[n - 1]$表示上一次没有在中间的 ...
- 2、变量var关键字
我们在看js声明变量的时候,经常会发现有的变量前会带var 但又的则没有,那么这究竟有什么区别呢? 如果这种情况发生在函数里的话, 加var定义的变量是局部变量,不加var定义的就成了全局变量. // ...
- ZooKeeper搭建
ZooKeeper系列之一:ZooKeeper简介 ZooKeeper 是一个为分布式应用所设计的分布的.开源的协调服务.分布式的应用可以建立在同步.配置管理.分组和命名等服务的更高级别的实现的基础之 ...
- 《JS权威指南学习总结--7.8 数组方法》
内容要点: 一.join() Array.join()方法将数组中所有元素都转化为字符串并连接在一起,返回最后生产的字符串. 可以指定一个可选的字符串在生产的字符串中来分隔数组的各个元素.如果不指定分 ...
- Effective JavaScript :第五章
1.使用Object的直接实例构造轻量级的字典 字典就是可变长的字符串与值得映射集合.JavaScript甚至提供了枚举一个对象属性名的利器——for...in循环. var dict = { ali ...
- 5. Java反射机制
Java反射机制 问题: 在运行时,对一个JAVA类,能否知道属性和方法:能否调用它的任意方法? 答案是可以的,JAVA提供一种反射机制可以实现. 目录 什么是JAVA的反射机制 JDK中提供的R ...