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

解题思路

先序遍历,递归的过程中保存路径,遇到叶子节点时将路径加入结果集。

实现代码

Java:

// Runtime: 3 ms
/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode(int x) { val = x; }* }*/
public class Solution {private List<String> paths = new ArrayList<String>();public List<String> binaryTreePaths(TreeNode root) {if (root != null) {traverse(root, String.valueOf(root.val));}return paths;}private void traverse(TreeNode root, String path) {if (root.left == null && root.right == null) {paths.add(path);}if (root.left != null) {traverse(root.left, path + "->" + root.left.val);}if (root.right != null) {traverse(root.right, path + "->" + root.right.val);}}
}

[LeetCode] Binary Tree Paths相关推荐

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

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

  2. LeetCode : Binary Tree Paths

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

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

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

  4. Binary Tree Paths leetcode

    Binary Tree Paths Given a binary tree, return all root-to-leaf paths. For example, given the followi ...

  5. [LeetCode] Binary Tree Level Order Traversal 二叉树层次遍历(DFS | BFS)

    目录: 1.Binary Tree Level Order Traversal - 二叉树层次遍历 BFS 2.Binary Tree Level Order Traversal II - 二叉树层次 ...

  6. CF750G New Year and Binary Tree Paths(数位dp二进制+数学)

    CF750G New Year and Binary Tree Paths description solution code description 题目链接 一颗无穷个节点的完全二叉树. 求有多少 ...

  7. Binary Tree Paths

    Binary Tree Paths Given a binary tree, return all root-to-leaf paths. For example, given the followi ...

  8. LeetCode OJ -- Binary Tree Paths

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

  9. LeetCode 257. Binary Tree Paths (二叉树路径)

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

最新文章

  1. swust oj 962
  2. JavaScript强化教程——JavaScript 运算符
  3. 程序员书单_UML篇
  4. Excel VBA 学习总结 - 数据验证与正则表达式
  5. python | 三种可变参数简述
  6. 我在微信上大学:如何正确理解指针和结构体指针?
  7. ef core mysql 生成迁移失败_EFCore + MySql codeFirst 迁移 Migration出现的问题
  8. 常见分数值归一化方法
  9. Flink - allowedLateness
  10. 虚拟机上键盘右边的数字小键盘为什么不能使用
  11. 第三阶段应用层——1.7 数码相册—电子书(6)—支持远程打印信息
  12. 服务器故障导致网站打不开,网站打不开的6种现象及解决方法
  13. oracle数据库查看防火墙,Oracle数据库防火墙简介
  14. Oracle 压缩表与压缩表空间
  15. python 两个等长list的各对应位置元素相加+两个字典相加,相同键元素累加,不同键元素取全集
  16. Windows环境下编译Airsim
  17. 《月之猎人 (Moon Hunters)》主角设计
  18. 【庖丁解牛系列】 项目时间管理之前导图/单代号网络图
  19. C#修改解决方案的名称 和解决方案文件夹的名称 ,及项目程序名称,项目文件夹名称
  20. OpenFOAM 中的 RTS 机制

热门文章

  1. 的watch什么时候触发_建筑结构丨泡面为什么是弯的,海带又为什么要打结?
  2. python程序设计实验报告实验程序流程序列化_Python使用pickle模块实现序列化功能示例...
  3. STM32F103ZET6开发板PB3/4,PA13/14/15等I/O口的特殊配置
  4. 【AI白身境】学深度学习你不得不知的爬虫基础
  5. 全球及中国自主运输机器人行业十四五创新模式与运行战略规划报告2022版
  6. 中国制鞋机械行业市场“十四五”规划模式及项目投资分析报告2022-2028年版
  7. Ubuntu下对双显卡的支持问题
  8. 智慧赋能黔货出山 丰收节交易会·李喜贵:贵州农业数字化
  9. 新疆兵团谋定丰收美景-万祥军:对话农民丰收节交易会
  10. Crontab和sudo中无法使用TensorFlow ImportError libcublas.so.9.0