作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/



题目地址:https://leetcode.com/problems/binary-tree-paths/#/description

题目描述

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

题目大意

但引出所有的从根节点到叶子节点的路径。

解题方法

递归

把二叉树的从根节点到叶子节点的每条路径都打印出来,实用的方法就是很简单的递归调用。如果是叶子就把这个路径保存到list中,如果不是叶子就把这个节点的值放入到path中,然后再继续调用,直到达到叶子节点为止。

我用StringBuilder的结果会糅杂在一起,就不能用,也没想明白为什么= =

java版本:

/**
* 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> binaryTreePaths(TreeNode root) {
List<String> ans = new ArrayList<String>();
if(root != null){
searchNode(root, "", ans);
}
return ans;
} public void searchNode(TreeNode root, String path, List<String> ans){
if(root.left == null && root.right == null){
ans.add(path + root.val);
}
if(root.left != null){
searchNode(root.left, path + root.val + "->", ans);
}
if(root.right != null){
searchNode(root.right, path + root.val + "->", ans);
}
}
}

===========二刷

把path作为字符串,res作为数组保存字符串。一般递归都可以这么写的。

python版本:

# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def binaryTreePaths(self, root):
"""
:type root: TreeNode
:rtype: List[str]
"""
if not root:
return []
res = []
self.dfs(root, res, '' + str(root.val))
return res def dfs(self, root, res, path):
if root.left == None and root.right == None:
res.append(path)
if root.left != None:
self.dfs(root.left, res, path + '->' + str(root.left.val))
if root.right != None:
self.dfs(root.right, res, path + '->' + str(root.right.val))

迭代

使用迭代的话,大家都知道需要用一个栈,那么感觉就是个模板题了,而且和上面的递归做法基本是一样的了。

# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def binaryTreePaths(self, root):
"""
:type root: TreeNode
:rtype: List[str]
"""
if not root:
return []
stack = []
res = []
stack.append((root, str(root.val)))
while stack:
node, path = stack.pop()
if not node.left and not node.right:
res.append(path)
if node.left:
stack.append((node.left, path + "->" + str(node.left.val)))
if node.right:
stack.append((node.right, path + "->" + str(node.right.val)))
return res

日期

2017 年 5 月 6 日
2018 年 2 月 25 日
2018 年 11 月 19 日 —— 周一又开始了

【LeetCode】257. Binary Tree Paths 解题报告(java & python)的更多相关文章

  1. Java [Leetcode 257]Binary Tree Paths

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

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

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

  3. [LeetCode] 257. Binary Tree Paths 二叉树路径

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

  4. 【LeetCode】919. Complete Binary Tree Inserter 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址: https://leetcode. ...

  5. 【LeetCode】814. Binary Tree Pruning 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 后序遍历 日期 题目地址:https://leetc ...

  6. Leetcode 257. Binary Tree Paths

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

  7. (easy)LeetCode 257.Binary Tree Paths

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

  8. [leetcode]257. Binary Tree Paths二叉树路径

    Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example ...

  9. LeetCode 257. Binary Tree Paths(二叉树根到叶子的全部路径)

    Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example ...

随机推荐

  1. python 封装、绑定

    目录 python 封装.绑定 1.数据.方法的封装 2.隐藏属性 3.开放接口 4.绑定方法 1.对象的绑定 2.类的绑定(classmethod) 3.非绑定方法(staticmethod) 4. ...

  2. day33 前端之css

    day33 前端之css css简介 CSS(Cascading Style Sheet,层叠样式表)定义如何显示HTML元素. # 语法结构 选择器 { 属性名1,属性值 属性名2,属性值 } # ...

  3. Azkaban(一)【集群安装】

    目录 一.下载解压 二. 配置Mysql 三. 配置Azkaban Executor 四. 配置Azkaban WebServer 一.下载解压 1.下载地址:https://github.com/a ...

  4. Linux学习 - 条件判断

    一.判断格式 test -e /root/install.log 或 [ -e /root/install.log ] 使用echo $?查看是否正确,当返回0时表示返回正确 1 按照文件类型进行判断 ...

  5. MyBatis Collection小记—— 关联查询、递归查询、多字段关联

    经常会用到mybatis的Collection标签来做级联查询或递归查询,现通过一个伪例来简单的说明一下使用中的关键点: 首先先列出三个表,给出一个场景: 1,角色表 t_role( id,name ...

  6. @FeignClient同一个name,多个配置类的解决方案

    概述   我使用的spring-cloud-starter-openfeign的版本是2.0.0,然后使用@FeignClient的时候是不能一个name多个配置类的,后来也是从网络查找了各种网友的方 ...

  7. UIImageView总结

    UIImageView UIKit框架提供了非常多的UI控件,但并不是每一个都很常用,有些控件可能1年内都用不上,有些控件天天用,比如UIButton.UILabel.UIImageView.UITa ...

  8. 【Linux】【Services】【DNS】使用Bind搭建DNS服务

    1. 简介 1.1. 实现的功能:DNS解析以及智能转发 1.2. 官方文档: 1.3. 基础概念:http://www.cnblogs.com/demonzk/p/6494968.html 2. 环 ...

  9. Docker从入门到精通(四)——常用命令

    话不多说,本篇文章给大家介绍 docker 的常用命令,基本上会覆盖我们日常使用的命令. 1.万能帮助命令 docker 命令 --help 假设你想用某个命令,但是又不知道该命令的一些参数怎么用,这 ...

  10. <转>单机版搭建Hadoop环境

    安装过程: 一.安装Linux操作系统 二.在Ubuntu下创建hadoop用户组和用户 三.在Ubuntu下安装JDK 四.修改机器名 五.安装ssh服务 六.建立ssh无密码登录本机 七.安装ha ...