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 ...
随机推荐
- Dubbo源码分析(5):ExtensionLoader
背景 Dubbo所有的模块加载是基于SPI机制的.在接口名的上一行加个@SPI注解表明要此模块要通过ExtensionLoader加载.基于SPI机制的扩展性比较好,在不修改原有代码,可以实现新模块的 ...
- Visual Studio 2017 许可证已过期解决方案
1.卸载并重安VS2017 2.安装后打开VS2017,点击帮助=>注册产品,输入序列号NJVYC-BMHX2-G77MM-4XJMR-6Q8QF(企业版), KBJFW-NXHK6-W4WJM ...
- 64位内核开发第十讲,IRQL中断级别了解
目录 中断级别IROL了解 一丶IRQL 1.了解什么是中断 2.IROL中断级别. 3.遵守IROL编程规范的方式 中断级别IROL了解 一丶IRQL 1.了解什么是中断 中断就是产生的一个电信号. ...
- 最大字段和&洛谷11月月赛DIV2 T1
蒟蒻只能打一打DIV2的基础题 太卑微了 这道题的本质其实是再建一个数组,如果s串i位置是0那么就给a[i]赋值为1,表示要累加个数,如果是1那么就把a[i]赋值为-1,表示个数减一,最后求最大子段和 ...
- Java 基础:单例模式 Singleton Pattern
1.简介 单例模式(Singleton Pattern)是 Java 中最简单的设计模式之一.这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式. 这种模式涉及到一个单一的类,该类负责创 ...
- 信竞四定律orz
正常代码不写#define @zdx 平时刷题不写freopen @liuziwen 循环内部不写return 0 @asdfo123 主程序内不写char array @asdfo123 输出时间: ...
- 小程序的基本原生js使用
1.点击事件 <a data-current="{{setting.current}}" bindtap="clickcurrent" style=&qu ...
- window.location.href在微信端不起作用的解决方法?
在我从第一张图的某个活动进去到详情页,点击返回是可以的,这里我是用了一个click事件,window.location.href="某死链接" 但是第二次进去点击之后点击事件是可以 ...
- cnetos7--zabbix(3.4)-server安装
1.安装前准备 (1)关闭防火墙 [root@localhost ~]#Systemctl stop firewalld.service (2)开机关闭防火墙 [root@localhost ~]#S ...
- tecplot不能导入fluent多面体的解决办法
多面体网格在fluent当中计算完成,如果保存cas和dat文件,如果直接将cas和dat文件导入tecplot进行后处理,tecplot会提示多面体网格在fluent当中计算完成,如果保存cas和d ...