二叉树的所有路径

给一棵二叉树,找出从根节点到叶子节点的所有路径。

样例

给出下面这棵二叉树:

   1
/ \
2 3
\
5

所有根到叶子的路径为:

[
"1->2->5",
"1->3"
] 解题
深度优先 可以转换成先序遍历:根左右,根结点遍历以后,遍历两个子树,是叶子结点的时候保存路径
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param root the root of the binary tree
* @return all root-to-leaf paths
*/
public ArrayList<String> binaryTreePaths(TreeNode root) {
// Write your code here
ArrayList<String> result = new ArrayList<String>();
if(root == null)
return result;
String path="";
Paths(root,result,path);
return result;
}
public void Paths(TreeNode root,ArrayList<String> result,String path){
if(root == null)
return;
if(root.left==null && root.right == null){
if( path.equals(""))
path += root.val;
else
path +="->"+root.val;
result.add(path);
return;
}
if( path.equals(""))
path += root.val;
else
path +="->"+root.val;
Paths(root.left,result,path);
Paths(root.right,result,path); }
}

后面两行代码互换对结果没有影响,只是所有路径的先后次序发生了变化

Paths(root.right,result,path);
Paths(root.left,result,path);
 

lintcode:二叉树的所有路径的更多相关文章

  1. [LeetCode] Binary Tree Maximum Path Sum 求二叉树的最大路径和

    Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...

  2. DS树+图综合练习--二叉树之最大路径

    题目描述 给定一颗二叉树的逻辑结构(先序遍历的结果,空树用字符‘0’表示,例如AB0C00D00),建立该二叉树的二叉链式存储结构 二叉树的每个结点都有一个权值,从根结点到每个叶子结点将形成一条路径, ...

  3. LintCode2016年算法比赛----二叉树的所有路径

    二叉树的所有路径 题目描述 给定一棵二叉树,找从根节点到叶子节点的所有路径 样例 给出下面这课二叉树: 1 / \ 2 3 \ 5 所有根到叶子的路径为: [ "1->2->5& ...

  4. [LeetCode] 124. Binary Tree Maximum Path Sum 求二叉树的最大路径和

    Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any ...

  5. 算法 dfs 二叉树的所有路径

    480. 二叉树的所有路径 给一棵二叉树,找出从根节点到叶子节点的所有路径. Example 样例 1: 输入:{1,2,3,#,5} 输出:["1->2->5",&q ...

  6. 75.Binary Tree Maximum Path Sum(二叉树的最大路径和)

    Level:   Hard 题目描述: Given a non-empty binary tree, find the maximum path sum. For this problem, a pa ...

  7. Java实现 LeetCode 257 二叉树的所有路径

    257. 二叉树的所有路径 给定一个二叉树,返回所有从根节点到叶子节点的路径. 说明: 叶子节点是指没有子节点的节点. 示例: 输入: 1 / \ 2 3 \ 5 输出: ["1->2 ...

  8. 【二叉树-所有路经系列(根->叶子)】二叉树的所有路径、路径总和 II、路径总和、求根到叶子节点数字之和(DFS)

    总述 全部用DFS来做 重点一:参数的设置:为Root,路径字符串,路径List集合. 重点二:步骤: 1 节点为null 2 所有节点的操作 3 叶子结点的操作 4 非叶节点的操作 题目257. 二 ...

  9. lintcode:二叉树的路径和

    题目 给定一个二叉树,找出所有路径中各节点相加总和等于给定 目标值 的路径. 一个有效的路径,指的是从根节点到叶节点的路径. 解题 下面有个小bug 最后比较的时候是叶子节点为空,左右都有叶子结点,所 ...

随机推荐

  1. [转]rpcndr.h和wtypes.h冲突Bug的解决方案

    [转]rpcndr.h和wtypes.h冲突Bug的解决方案 http://blog.csdn.net/tzwh_86/article/details/9495133 rpcndr.h和wtypes. ...

  2. Entity Framework 学习第一天

    文章是作为初学者记录之用,没有学习过的同学可以借鉴一下,至于用过和高手嘛,就算了吧.仅是入门.废话不多说了,马上新建个项目,添加Entity Framework,这个词以下将用EF代替. 本文使用的I ...

  3. LinQ[持续更新]

    var query = from t1_row in eprg.AttestationConditions where t1_row.Attestation.ProviderNumber == pro ...

  4. PBOC2.0与3.0的区别

    一.PBOC规范颁布的历程 1997年12月,PBOC V1.0  定义了五个方面的事项  电子钱包/电子存折应用(EP,ED)  卡片和终端的接口  卡片本身的技术指标  应用相关的交易流程  终端 ...

  5. Android BLE API: GATT Notification not received

    When setting the value to the descriptor instead of putting descriptor.setValue(BluetoothGattDescrip ...

  6. 团队项目的NABC(截图软件)

    我们小组要开发的软件是基于windows上的截图软件,其NABC如下. 1.need: 首先,截图工具是几乎每个用户都会用到的,不管是在工作中,还是在学习上,其针对的人群从学生到上班族不等, 所以,他 ...

  7. linux入门基础_centos(一)--基础命令和概念

    闲来无事干,看看2014自己整理的一些学习笔记.独乐了不如众乐乐吗! 贴出来和大家分享一下,由于篇幅比较长,分成几篇发布吧,由于是学习笔记,可能有些地方写的不是很正确或者说不详细,或者你会看到上面的课 ...

  8. 第六周博客技术发表 C语言代码

    #include <stdio.h>       /*使用printf要包含的头文件*/#include <conio.h>void main(void)        /*主 ...

  9. 关于Web服务器域名设置相关知识积累

    1.第一个问题,如何将一个服务器映射到一个域名上呢?    在申请域名的时候,会配置服务器IP和域名的对应关系,所以如果系统中只有一个应用的情况下,应用服务器不需要做任何配置. 2.在Tomcat服务 ...

  10. RabbitMQ 3.6 安装

    1. 首先安装这个 http://www.erlang.org/downloads 2. 再安装这个 http://www.rabbitmq.com/install-windows.html 3. 添 ...