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 ...
随机推荐
- spring框架面試題目
25个经典的Spring面试问答 这是在网上下载的面试题,忘记了出处,如带来不便联系本人立马删除,在这里提供给将要面试的朋友,与大家分享,希望能给您带来帮助! 问题清单: 1. 什么是Spring框架 ...
- URI与URN与URL详解
当没有URI时 什么是URI和URN和URL URI详解 Uniform Resource Identifier 统一资源标识符 URI的组成 案例: https://tools.ietf.org/h ...
- POJ 3233 Matrix Power Series——快速幂&&等比&&分治
题目 给定一个 $n \times n$ 的矩阵 $A$ 和正整数 $k$ 和 $m$.求矩阵 $A$ 的幂的和. $$S = A + A^2 + ... + A^k$$ 输出 $S$ 的各个元素对 ...
- 项目架构&架构部署&网站分析&网站优化
一.架构演变 一个项目至少由三层内容组成:web访问层.数据库层.存储层 初级阶段 单体阶段 常见场景:项目初期 部署特点:所有应用服务都在一台主机 应用特点:开发简单 应用/数据分离阶段 常见场景: ...
- BCB key事件中判断Shift、Alt、Ctrl状态
BCB key事件中判断Shift.Alt.Ctrl状态: 类似此事件中 void __fastcall TForm1::keydown(TObject *Sender, WORD &Key, ...
- C语言十六进制转换成十进制:要从右到左用二进制的每个数去乘以16的相应次方
#include <stdio.h> /* 十六进制转换成十进制:要从右到左用二进制的每个数去乘以16的相应次方: 在16进制中:a(A)=10 b(B)=11 c(C)=12 d(D)= ...
- pcl-设置多线段宽度和颜色
显示点云有使用vtk的,有使用 ros 中riz ?库的,使用pcl显示点云数据比较方便,但是对于一些模型形状只能固定特定的效果,比如说直线段,只能绘制点到点两点之间的线段.但是项目需要绘制点1到点2 ...
- Magma中ECC的点乘实例
a:=-3;b:= 41058363725152142129326129780047268409114441015993725554835256314039467401291;E:= Elliptic ...
- linux rand application
code: #include <stdio.h> #include <stdlib.h> #include <time.h> #define random_1(a, ...
- 【软工实践】Alpha冲刺(1/6)
链接部分 队名:女生都队 组长博客: 博客链接 作业博客:博客链接 小组内容 恩泽(组长) 过去两天完成了哪些任务 描述 了解了反馈机制的实现原理 确定好算法的框架 对接口的规范化进行学习 展示Git ...