LeetCode 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"]
题目标签:Tree
这道题目给了我们一个二叉树,让我们记录所有的路径,返回一个array string list。 我们可以另外设一个findPaths function, 代入参数是node 和 String path。在设一个List<String> res 在两个funciton之外。
遍历所有的点,对于每一个点:
1。如果这个点是leaf node, 到底了,那么添加path 进res。
2。如果这个点还有left child,那么递归代入node.left 和 path + "->" + left的值。
3。如果这个点还有right child, 那么递归代入node.right 和 path + "->" + right的值。
findPaths function也不需要return,因为如果到底了,直接加入这个path就可以了,它也不会继续递归代入了。
Java Solution:
Runtime beats 68.03%
完成日期:07/05/2017
关键词:Tree
关键点:设一个function代入参数值是node 和 String path , 利用path 来传递每一次的点
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution
{
List<String> res = new ArrayList<String>(); public List<String> binaryTreePaths(TreeNode root)
{
if(root == null)
return res; findPaths(root, String.valueOf(root.val)); return res;
} public void findPaths(TreeNode node, String path)
{
if(node.left == null && node.right == null)
res.add(path); if(node.left != null)
findPaths(node.left, path + "->" + node.left.val); if(node.right != null)
findPaths(node.right, path + "->" + node.right.val); }
}
参考资料:
https://segmentfault.com/a/1190000003465753
LeetCode 算法题目列表 - LeetCode Algorithms Questions List
LeetCode 257. Binary Tree Paths (二叉树路径)的更多相关文章
- [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二叉树路径
Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example ...
- Leetcode 257 Binary Tree Paths 二叉树 DFS
找到所有根到叶子的路径 深度优先搜索(DFS), 即二叉树的先序遍历. /** * Definition for a binary tree node. * struct TreeNode { * i ...
- [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(二叉树根到叶子的全部路径)
Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example ...
- [LeetCode] Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- 257 Binary Tree Paths 二叉树的所有路径
给定一个二叉树,返回从根节点到叶节点的所有路径.例如,给定以下二叉树: 1 / \2 3 \ 5所有根到叶路径是:["1->2->5", " ...
- 【easy】257. Binary Tree Paths 二叉树找到所有路径
http://blog.csdn.net/crazy1235/article/details/51474128 花样做二叉树的题……居然还是不会么…… /** * Definition for a b ...
- Leetcode 257. Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
随机推荐
- PKI信息安全知识点详细解答包含HTTPS
1. 什么是X.509? X.509标准是ITU-T设计的PKI标准,他是为了解决X.500目录中的身份鉴别和访问控制问题设计的. 2. 数字证书 数字证书的意义在于回答公钥属于谁的问题,以帮助用户安 ...
- CVE-2016-10191 FFmpeg RTMP Heap Buffer Overflow 漏洞分析及利用
作者:栈长@蚂蚁金服巴斯光年安全实验室 一.前言 FFmpeg是一个著名的处理音视频的开源项目,使用者众多.2016年末paulcher发现FFmpeg三个堆溢出漏洞分别为CVE-2016-10190 ...
- 【DDD】领域驱动设计实践 —— UI层实现
前面几篇blog主要介绍了DDD落地架构及业务建模战术,后续几篇blog会在此基础上,讲解具体的架构实现,通过完整代码demo的形式,更好地将DDD的落地方案呈现出来.本文是架构实现讲解的第一篇,主要 ...
- 百度富文本编辑器Ueditor使用
首先我们登上ueditor下载,可以看到多种版本. UBuilder:可以自己选择需要的工具. 我用的开发版,Java的jsp版本,在这里是全部工具,但是工具在配置文件中也是可以自己选择的. 下载下来 ...
- java基础解析系列(七)---ThreadLocal原理分析
java基础解析系列(七)---ThreadLocal原理分析 目录 java基础解析系列(一)---String.StringBuffer.StringBuilder java基础解析系列(二)-- ...
- javascript插入before(),after()新DOM方法
随着web的技术突飞猛进的发展.HTML5 ES6等新技术的发展,与此同时DOM等标准也在悄悄的进步,各大浏览器也在悄悄的发展适配新的属性和方法,今天我们来看看Javascript新的DOM的方法 二 ...
- Java 基础语法
一.关键字 定义:被 Java 赋予特殊含义的单词. 特点:基本上都是英文小写. 用于定义数据类型的关键字 基本数据类型 整数型(默认为 int) byte(8 位,1 字节,默认值是 0,最大存储数 ...
- E. Fish (概率DP)
E. Fish time limit per test 3 seconds memory limit per test 128 megabytes input standard input outpu ...
- 如何用Django从零开始搭建一个网站(0)
python,django等安装就直接略过了.下面直接奔如主题,搭建网站. Step1:新建一个django project,运行命令:‘django-admin startproject myPit ...
- python 的日志logging模块学习
1.简单的将日志打印到屏幕 import logging logging.debug('This is debug message') logging.info('This is info messa ...