[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:
Input: 1
/ \
2 3
\
5 Output: ["1->2->5", "1->3"] Explanation: All root-to-leaf paths are: 1->2->5, 1->3
题目
给定一棵二叉树,求出所有根到叶的路径。
思路
DFS, Very straightforward
代码
class Solution {
public List<String> binaryTreePaths(TreeNode root) {
List<String> result = new ArrayList<>();
if(root == null) return result;
helper(root, result, "");
return result;
} private void helper (TreeNode root, List<String> result, String path){
// leaf node
if(root.left ==null && root.right ==null) {
result.add(path + root.val);
}
// dfs
if(root.left!=null){
helper(root.left, result, path+root.val+ "->");
}
if(root.right!=null){
helper(root.right, result, path+root.val+"->");
}
}
}
[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 二叉树 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. 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] 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 ...
随机推荐
- Spring MVC springMVC-servlet.xml
DispatcherServlet的配置: DispatcherServlet是SpringMVC的前端控制器,所有的请求都经过前端控制器,也是项目中出现的唯一一个servlet,在 web.xml中 ...
- html 更新
HTML介绍 Web服务本质 import socket sk = socket.socket() sk.bind(("127.0.0.1", 8080)) sk.listen(5 ...
- python基础补充内容
知识内容: 1.三元运算表达式 2.python代码编写规范 3.模块导入与使用 4.python文件名 5.python脚本的"__name__"属性 6.python之禅 一. ...
- html文字在django模板中取消转译
django {{ news.content | safe }} 还有escape=fales,具体咋用,有点蒙蔽
- 最小生成树-kruskal
kruskal算法,没有进行算法复杂度分析 判断俩个结点是否在同一个树上使用了dfs,比较low的写法 输入数据 //第一行,结点数,结点数,边数 9 9 14a b 4b c 8c d 7a h 8 ...
- docker使用笔记1
rhel6安装 yum -y install docker-io ################################################ 进入容器命令 docker exec ...
- springboot sybase 数据库
依赖:(驱动) <!-- https://mvnrepository.com/artifact/net.sourceforge.jtds/jtds --> <dependency&g ...
- C# ADO.NET 封装的增删改查
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- leetcode925
public class Solution { public bool IsLongPressedName(string name, string typed) { var list1 = new L ...
- 简单的socket_server 和 socket_client(实现文件的上传功能)
socket_server 客户端程序 import socket, os, json class Ftcplient(object): def __init__(self): "" ...