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"]

题目标签:Tree

  这道题目给了我们一个二叉树,让我们记录所有的路径,返回一个array string list。 我们可以另外设一个findPaths function, 代入参数是node 和 String path。在设一个List<String> res 在两个funciton之外。

  遍历所有的点,对于每一个点:

    1。如果这个点是leaf node, 到底了,那么添加path 进res。

    2。如果这个点还有left child,那么递归代入node.left 和 path + "->" + left的值。

    3。如果这个点还有right child, 那么递归代入node.right 和 path + "->" + right的值。

findPaths function也不需要return,因为如果到底了,直接加入这个path就可以了,它也不会继续递归代入了。

Java Solution:

Runtime beats 68.03%

完成日期:07/05/2017

关键词:Tree

关键点:设一个function代入参数值是node 和 String path , 利用path 来传递每一次的点

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 public class Solution
11 {
12     List<String> res = new ArrayList<String>();
13
14     public List<String> binaryTreePaths(TreeNode root)
15     {
16         if(root == null)
17             return res;
18
19         findPaths(root, String.valueOf(root.val));
20
21         return res;
22     }
23
24     public void findPaths(TreeNode node, String path)
25     {
26         if(node.left == null && node.right == null)
27             res.add(path);
28
29         if(node.left != null)
30             findPaths(node.left, path + "->" + node.left.val);
31
32         if(node.right != null)
33             findPaths(node.right, path + "->" + node.right.val);
34
35
36     }
37 }

参考资料:

https://segmentfault.com/a/1190000003465753

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

转载于:https://www.cnblogs.com/jimmycheng/p/7123123.html

LeetCode 257. Binary Tree Paths (二叉树路径)相关推荐

  1. 【easy】257. Binary Tree Paths 二叉树找到所有路径

    http://blog.csdn.net/crazy1235/article/details/51474128 花样做二叉树的题--居然还是不会么-- /*** Definition for a bi ...

  2. LeetCode 257 Binary Tree Paths

    题目描述 Given a binary tree, return all root-to-leaf paths. For example, given the following binary tre ...

  3. LeetCode刷题记录14——257. Binary Tree Paths(easy)

    LeetCode刷题记录14--257. Binary Tree Paths(easy) 目录 前言 题目 语言 思路 源码 后记 前言 数据结构感觉理论简单,实践起来很困难. 题目 给定一个二叉树, ...

  4. [LeetCode] Binary Tree Paths - 二叉树基础系列题目

    目录: 1.Binary Tree Paths - 求二叉树路径 2.Same Tree - 判断二叉树相等 3.Symmetric Tree - 判断二叉树对称镜像 Binary Tree Path ...

  5. LeetCode OJ -- Binary Tree Paths

    http://blog.ubooksapp.com/ 标签(空格分隔): LeetCode OJ BinaryTree Given a binary tree, return all root-to- ...

  6. 257. Binary Tree Paths

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1/ ...

  7. leetcode 662. Maximum Width of Binary Tree | 662. 二叉树最大宽度(BFS)

    题目 https://leetcode.com/problems/maximum-width-of-binary-tree/ 题解 本题思路来源于二叉树的层序遍历. 层序遍历类似问题:leetcode ...

  8. 【二叉树的迭代版后序遍历】LeetCode 145. Binary Tree Postorder Traversal

    LeetCode 145. Binary Tree Postorder Traversal Solution1:递归版答案 二叉树的后序遍历递归版是很简单的,关键是迭代版的代码既难理解又难写!但听了花 ...

  9. 【二叉树迭代版中序遍历】LeetCode 94. Binary Tree Inorder Traversal

    LeetCode 94. Binary Tree Inorder Traversal Solution1:递归版 二叉树的中序遍历递归版是很简单的,中序遍历的迭代版需要特殊记一下! 迭代版链接:htt ...

最新文章

  1. IE8下不识别indexOf的问题
  2. snp可视化之瀑布图
  3. 成功解决 ERROR: An error occurred while performing the step: “Building kernel modules“. See  /var/log/nv
  4. 网站外链查询接近100%精确的方法
  5. 这些棘手的Java面试题,答案你都知道吗?
  6. mysql null 0 空_MySQL中 null与not null和null与空值''的区别
  7. SQL varchar数据类型深入探讨
  8. 在SourceForge中建立开源项目
  9. [PsTools]psexec.exe使用范例-运行远程电脑程序(exe、bat等)
  10. 银行计算机知识,银行考试计算机知识试题及答案
  11. Kubernetes快速部署,kubectl命令使用,资源管理
  12. win10安装wget,从此可以更快的下载文件 and windows10 下 zip命令行参数详解
  13. springboot版的微信公众号,订阅号
  14. java获取行政区划编码(省市区县居委5级)
  15. sgx芯片服务器,英特尔SGX概述:SGX内部实现分析研究(part1)
  16. 物料移动类型和后勤自动科目设置-转
  17. java 模块解耦_微服务架构:如何用十步解耦你的系统?
  18. Redis基于Set如何实现用户关注模型?
  19. 使用ExcelJs导出表格设置样式、添加边框
  20. VRRP 出现部分双主情况

热门文章

  1. 用python进行归并排序,用Python做归并排序
  2. js 高级 constructor构造函数
  3. jinja Template
  4. 网络通信 route(公司局域网配置)
  5. 数据结构之基于Java的顺序列表实现
  6. C语言scanf函数详解和示例
  7. win与Linux的防火墙配置
  8. 大咖博闻荟 | 基于NSX-T和AVI实现企业双活中心
  9. 一步步实现SDDC-分布式交换机入门
  10. Java基础学习总结(152)——JDK 1.7和 JDK 1.8中HashMap的实现有什么不同?