LintCode2016年算法比赛----二叉树的所有路径
二叉树的所有路径
题目描述
给定一棵二叉树,找从根节点到叶子节点的所有路径
样例
给出下面这课二叉树:
1
/ \
2 3
\
5
所有根到叶子的路径为:
[
"1->2->5",
"1->3"
]
算法分析:
递归地处理二叉树,先将子树的所有路径求出,然后把根节点的数据贴到子路径的所有结果上
Java算法实现:
/**
* 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 List<String> binaryTreePaths(TreeNode root) {
// Write your code here
List<String>list=new ArrayList<>();
if(root!=null){
List<String>left=binaryTreePaths(root.left);
List<String>right=binaryTreePaths(root.right);
String current=String.valueOf(root.val);
if(left.size()==0&&right.size()==0){
list.add(current);
}
else{
if(left.size()!=0){
for(String leftStr:left){
list.add(current+"->"+leftStr);
}
}
if(right.size()!=0){
for(String rightStr:right){
list.add(current+"->"+rightStr);
}
}
}
}
return list;
}
}
LintCode2016年算法比赛----二叉树的所有路径的更多相关文章
- 算法 dfs 二叉树的所有路径
480. 二叉树的所有路径 给一棵二叉树,找出从根节点到叶子节点的所有路径. Example 样例 1: 输入:{1,2,3,#,5} 输出:["1->2->5",&q ...
- [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. ...
- lintcode:二叉树的所有路径
二叉树的所有路径 给一棵二叉树,找出从根节点到叶子节点的所有路径. 样例 给出下面这棵二叉树: 1 / \ 2 3 \ 5 所有根到叶子的路径为: [ "1->2->5" ...
- DS树+图综合练习--二叉树之最大路径
题目描述 给定一颗二叉树的逻辑结构(先序遍历的结果,空树用字符‘0’表示,例如AB0C00D00),建立该二叉树的二叉链式存储结构 二叉树的每个结点都有一个权值,从根结点到每个叶子结点将形成一条路径, ...
- A* 算法求第k短路径
A*算法是一类贪心算法,其可以用于寻找最优路径.我们可以利用A*算法来求第k短路径. 一条路径可以由两部分组成,第一部分是一个从出发到达任意点的任意路径,而第二部分是从第一部分的末端出发,到终点的最短 ...
- [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 ...
- 75.Binary Tree Maximum Path Sum(二叉树的最大路径和)
Level: Hard 题目描述: Given a non-empty binary tree, find the maximum path sum. For this problem, a pa ...
- Java实现 LeetCode 257 二叉树的所有路径
257. 二叉树的所有路径 给定一个二叉树,返回所有从根节点到叶子节点的路径. 说明: 叶子节点是指没有子节点的节点. 示例: 输入: 1 / \ 2 3 \ 5 输出: ["1->2 ...
- 【二叉树-所有路经系列(根->叶子)】二叉树的所有路径、路径总和 II、路径总和、求根到叶子节点数字之和(DFS)
总述 全部用DFS来做 重点一:参数的设置:为Root,路径字符串,路径List集合. 重点二:步骤: 1 节点为null 2 所有节点的操作 3 叶子结点的操作 4 非叶节点的操作 题目257. 二 ...
随机推荐
- BiliBili, ACFun… And More!【递归算法】
题源:http://acm.uestc.edu.cn/#/problem/show/3 题解: 题意:播放一段视频文件,有播放速度和缓冲速度两种,因为作者的癖好,播放前要缓冲几秒钟(这段时间不计算在总 ...
- 01背包-记忆化搜索到成型的DP
记忆化搜索 #include<bits/stdc++.h> using namespace std; typedef long long ll; int n,W; int dp[105][ ...
- MongoDB ver 4 几个常用命令
1. 为某个数据库创建用户: use db_test1; db.createUser({ user:"test_user_1", pwd:"test_user_1_pwd ...
- (转)OpenStack —— 原理架构介绍(一、二)
原文:http://blog.51cto.com/wzlinux/1961337 http://blog.51cto.com/wzlinux/category18.html-------------O ...
- 1.TypeError: must be str, not bytes
1.TypeError: must be str, not bytes错误: 解答: 写文件处 open(filename, 'w').write 应该写为 open(filename, 'wb'). ...
- JDK中ClassLoader的分类以及ClassLoader间的层次关系
几个常见的ClassLoader: bootstrap class loader: 最早启动的class loader,一般使用C语言,汇编语言,或是c++写的,用操作系统本地语言写的.这个cl ...
- php array_flip() 删除数组重复元素
在PHP中,用于删除数组中重复元素有一个可用的函数,那就是 array_unique(), 但是它并不是一个最高效的方法,使用array_flip() 函数将比array_uniqure()在速度上高 ...
- CentOS6.4安装OpenSSL
1.下载 wget https://www.openssl.org/source/openssl-1.0.2h.tar.gz 2.解压 tar zxf openssl-1.0.2h.tar.gz cd ...
- JVM的类加载时机
类加载过程中每个步骤的顺序 我们已经知道,类加载的过程包括:加载.连接.初始化,连接又分为:验证.准备.解析,所以说类加载一共分为5步:加载.验证.准备.解析.初始化. 其中加载.验证.准备.初始化的 ...
- Veloce2 Emulator
High capacity, high-speed, multi-application powerhouse for simulation and emulation of SoC designs ...