LeetCode_257. Binary Tree Paths
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
package leetcode.easy; /**
* Definition for a binary tree node. public class TreeNode { int val; TreeNode
* left; TreeNode right; TreeNode(int x) { val = x; } }
*/
public class BinaryTreePaths {
java.util.List<String> list = new java.util.LinkedList<>(); public java.util.List<String> binaryTreePaths(TreeNode root) {
path(root, "");
return list;
} private void path(TreeNode root, String str) {
if (null == root) {
return;
}
str = str + "->" + root.val;
if (null == root.left && null == root.right) {
list.add(str.substring(2));
return;
}
path(root.left, str);
path(root.right, str);
} @org.junit.Test
public void test() {
TreeNode node11 = new TreeNode(1);
TreeNode node21 = new TreeNode(2);
TreeNode node22 = new TreeNode(3);
TreeNode node32 = new TreeNode(5);
node11.left = node21;
node11.right = node22;
node21.left = null;
node21.right = node32;
node22.left = null;
node22.right = null;
node32.left = null;
node32.right = null;
System.out.println(binaryTreePaths(node11));
}
}
LeetCode_257. Binary Tree Paths的更多相关文章
- [LintCode] Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths.Example Given the following binary tree: 1 / \2 ...
- LintCode Binary Tree Paths
Binary Tree Paths Given a binary tree, return all root-to-leaf paths. Given the following binary tre ...
- 【LeetCode】257. Binary Tree Paths
Binary Tree Paths Given a binary tree, return all root-to-leaf paths. For example, given the followi ...
- <LeetCode OJ> 257. Binary Tree Paths
257. Binary Tree Paths Total Accepted: 29282 Total Submissions: 113527 Difficulty: Easy Given a bina ...
- [LeetCode] Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- leetcode : 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. For example, given the following binary tree: 1 ...
- Binary Tree Paths
Description: Given a binary tree, return all root-to-leaf paths. For example, given the following bi ...
- (easy)LeetCode 257.Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
随机推荐
- celery retry发送的队列
celery retry时,将发送到原有的队列,并在任务中加入执行的时间,以及当前重试的次数. worker立刻读取该任务,同时记录一条[任务]到 unacked中,表示该任务在worker中还未执行 ...
- Wiki with Alpha
Problem G. Wiki with AlphaInput file: standard input Time limit: 1 secondOutput file: standard outpu ...
- [React] Use the React Effect Hook in Function Components
Similar to the State Hook, the Effect Hook is “first-class” in React and handy for performing side e ...
- map json 字符串 对象之间的相互转化
1.对象与字符串之间的互转 将对象转换成为字符串 String str = JSON.toJSONString(infoDo); 字符串转换成为对象 InfoDo infoDo = JSON.pars ...
- loj #6191. 「美团 CodeM 复赛」配对游戏 期望dp
题意:有一个栈,随机插入 $n$ 次 $0$/$1$ 如果栈顶是 $1$,然后插入 $0$,则将这两个元素都弹出,否则,插入栈顶. 求:$n$ 次操作后栈中期望的元素个数. 我们发现,按照上述弹栈方式 ...
- web表单
1.配置 使用Flask-WTF, 它集成了WTForms并且完美地集成到了flask. 在microblog根目录下创建一个文件,存储flask扩展的所有配置,CSRF_ENABLED用于激活跨站点 ...
- PHP实现系统编程(一) --- 网络Socket及IO多路复用【网摘】
一直以来,PHP很少用于socket编程,毕竟是一门脚本语言,效率会成为很大的瓶颈,但是不能说PHP就无法用于socket编程,也不能说PHP的socket编程性能就有多么的低,例如知名的一款PHP ...
- 题解 UVA11105 【H-半素数 Semi-prime H-numbers】
做这道题之前首先要掌握的是线性筛的模板 附上题目链接 https://www.luogu.org/problem/P3383 首先这道题目的范围有些特殊必须是% 4 == 1的数才行 所以可以这样 直 ...
- 10分钟用Python爬取最近很火的复联4影评
欲直接下载代码文件,关注我们的公众号哦!查看历史消息即可! <复仇者联盟4:终局之战>已经上映快三个星期了,全球票房破24亿美元,国内票房破40亿人民币. 虽然现在热度逐渐下降,但是我们还 ...
- A. Vasya and Book ( Codeforces Educational Codeforces Round 55 )
题意:当前在看书的第 x 页,每次可以向前或者向后翻 d 页,这个书一共 n 页,问能否用最小操作翻到第 y 页. 题解:三种情况:1.直接翻能到的一定最短. 2.先翻到第一页,然后往后翻,翻到第 y ...