LeetCode——Binary Tree Paths
Description:
Given a binary tree, return all root-to-leaf paths.
For example, given the following binary tree:
1
/ \
2 3
\
5
All root-to-leaf paths are:
["1->2->5", "1->3"]
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<String> paths;
public List<Integer> path;
public List<String> binaryTreePaths(TreeNode root) {
paths = new ArrayList<String>();
path = new ArrayList<Integer>();
getAllPath(root); return paths;
} public void getAllPath(TreeNode node) { // 1
// / \
// 2 3 ["1->2->5", "1->3"]
// \
// if(node == null) {
return ;
}
path.add(node.val);
if(node.left==null && node.right==null) {
StringBuilder onePath = new StringBuilder();
for(int i=0; i<path.size(); i++) {
if(i != 0) onePath.append("->");
onePath.append(path.get(i));
}
paths.add(onePath.toString());
}
getAllPath(node.left);
getAllPath(node.right);
path.remove(path.size() - 1); }
}
LeetCode——Binary Tree Paths的更多相关文章
- [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 ...
- Python3解leetcode Binary Tree Paths
问题描述: Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. E ...
- LeetCode Binary Tree Paths(简单题)
题意: 给出一个二叉树,输出根到所有叶子节点的路径. 思路: 直接DFS一次,只需要判断是否到达了叶子,是就收集答案. /** * Definition for a binary tree node. ...
- leetcode Binary Tree Paths python
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...
- 【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 ...
- [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
257. Binary Tree Paths Easy Given a binary tree, return all root-to-leaf paths. Note: A leaf is a no ...
随机推荐
- Android——对话框2(日期和时间对话框)
xml <Button android:layout_width="match_parent" android:layout_height="wrap_conten ...
- java——泛型1(转)
一. 泛型概念的提出(为什么需要泛型)? 首先,我们看下下面这段简短的代码: 1 public class GenericTest { 2 3 public static void main(Stri ...
- kafkaStream执行过程中出现TimeoutException异常退出
日志中出现以下异常信息,程序中断退出. 目前参考别人的修改下面的配置,原来使用的hostname,改成IP,再观察观察. advertised.listeners=PLAINTEXT://192.1 ...
- oracle sql生成日历表
以下是生成2017年日历表: insert into dw_mdl.m_hadp_dim_date select to_char(everyDay,'yyyy-mm-dd') as dt, to_ch ...
- hadoop的调试
折腾hadoop的调试很久了,一直都没折腾对,查过很多资料,但是都没试出来,最终在不断地尝试当中调试出来了,所以想把这个过程记录下来,和大家分享一下. 调试分为两部分,MapReduce的调试和源码的 ...
- selenium测试(Java)--关闭窗口(二十)
quit方法:退出相关的驱动程序和关闭所有窗口 close方法:关闭当前窗口 package com.test.closewindow; import java.util.Iterator; impo ...
- Android XmlPullParser 笔记
使用XmlPullParser解析xml文件. 要解析的xml文件如下所示. weather.xml <?xml version="1.0" encoding="u ...
- 写给大忙人的JavaSE 8 - 学习
前面有提到过lambda和函数式接口,但是JavaSE 8 除了这两个新特性之后还提供了很多有用的东西.例如Stream. 摸索了几天,终于弄明白Stream的应用了. 先推荐一篇文章:Java 8 ...
- RequireJS使用小结1——for Effective JavaScript Module Loading
1. require和define的区别 The require() function is used to run immediate functionalities, while define() ...
- PHP删除目录及目录下所有文件或删除指定文件
PHP删除目录及目录下所有文件或删除指定文件 <?php header("content-type:text/html;charset=utf-8"); /** * 删除目录 ...