【LeetCode】257. Binary Tree Paths 解题报告(java & python)
作者: 负雪明烛
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)的更多相关文章
- Java [Leetcode 257]Binary Tree Paths
题目描述: Given a binary tree, return all root-to-leaf paths. For example, given the following binary tr ...
- LeetCode 257. 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 ...
- 【LeetCode】919. Complete Binary Tree Inserter 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址: https://leetcode. ...
- 【LeetCode】814. Binary Tree Pruning 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 后序遍历 日期 题目地址:https://leetc ...
- Leetcode 257. Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- (easy)LeetCode 257.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. Note: A leaf is a node with no children. Example ...
- 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 ...
随机推荐
- 打造基于 PostgreSQL/openGauss 的分布式数据库解决方案
在 MySQL ShardingSphere-Proxy 逐渐成熟并被广泛采用的同时,ShardingSphere 团队也在 PostgreSQL ShardingSphere-Proxy 上持续发力 ...
- 给lua_close实现回调函数
先讲下为什么会需要lua_close回调吧. 我用C++给lua写过不少库,其中有一些,是C++依赖堆内存,并且是每一个lua对象使用一块单独的内存来使用的. 在之前,我一直都是魔改lua源代码,给l ...
- 使用SpringBoot实现文件的下载
上一篇博客:使用SpringBoot实现文件的上传 已经实现了文件的上传,所以紧接着就是下载 首先还是html页面的简单设计 <form class="form-signin" ...
- A Child's History of England.20
CHAPTER 7 ENGLAND UNDER HAROLD THE SECOND, AND CONQUERED BY THE NORMANS Harold was crowned King of E ...
- REMI源安装php7.3
参考:https://blog.csdn.net/Phplayers/article/details/100901352 php5.6安装参考:https://www.cnblogs.com/Easo ...
- java加密方式
加密,是以某种特殊的算法改变原有的信息数据,使得未授权的用户即使获得了已加密的信息,但因不知解密的方法,仍然无法了解信息的内容.大体上分为双向加密和单向加密,而双向加密又分为对称加密和非对称加密(有些 ...
- 3.7 rust 静态块
Cargo.toml [dependencies] lazy_static = "1.4.0" main.rs #[macro_use] extern crate lazy_sta ...
- spring jdbc 配置数据源连接数据库
概述 在XML配置数据源,并将数据源注册到JDBC模板 JDBC模板关联业务增删改查 在XML配置数据源 <?xml version="1.0" encoding=" ...
- vue-cli安装记录
docker安装 docker network rm mydkdocker network create --subnet=192.168.1.0/24 mydk cat centos-7-x86_ ...
- 基于war的Spring Boot工程
一.简介 前面创建的Spring Boot工程最终被打为了Jar包,是以可执行文件的形式出现的,其使用了Spring Boot内嵌的Tomcat作为Web服务器来运行web应用的.新版Dubbo的监控 ...