Python3解leetcode Binary Tree Paths
问题描述:
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
思路:
二叉树的问题,首先考虑递归算法,用深度优先搜索。
为了体现模块化思想,一般讲DFS算法单独写成一个方法
代码:
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution:
def dfs(self,res,root,list1):
if root.left == None and root.right == None:#当前节点是叶子节点
res.append( '->'.join(list1))
return
if root.left != None:#左边不空
list1.append(str(root.left.val))
self.dfs(res,root.left,list1)
list1.pop()
if root.right != None:#右边不空
list1.append(str(root.right.val))
self.dfs(res,root.right,list1)
list1.pop() def binaryTreePaths(self, root):
"""
:type root: TreeNode
:rtype: List[str]
"""
if root == None:
return []
res = []
self.dfs(res,root,[str(root.val)])
return res
由于用list作为参数时,list参数可以视为全局变量,这样在任意一个层次的调用中,对list的更改都会影响所有层次调用中的list。
class Solution:
def dfs(self,res,root,str1):
if root.left == None and root.right == None:#当前节点是叶子节点
res.append(str1)
return str1 = str1 + '->'
if root.left != None:#左边不空
self.dfs(res,root.left,str1+str(root.left.val))
if root.right != None:#右边不空
self.dfs(res,root.right,str1+str(root.right.val)) def binaryTreePaths(self, root):
"""
:type root: TreeNode
:rtype: List[str]
"""
if root == None:
return []
res = []
self.dfs(res,root,str(root.val))
return res
上述代码用str传递参数,即为当前调用的局部参数,这时候就不需要每次调用完dfs后再pop()一次了,整体代码简洁一些
res仍旧是传递地址,可视为全局变量
Python3解leetcode Binary Tree Paths的更多相关文章
- Python3解leetcode Binary Tree PathsAdd DigitsMove Zeroes
问题描述: Given an array nums, write a function to move all 0's to the end of it while maintaining the r ...
- Python3解leetcode Binary Tree PathsAdd Digits
问题描述: Given a non-negative integer num, repeatedly add all its digits until the result has only one ...
- [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 ...
- LeetCode——Binary Tree Paths
Description: Given a binary tree, return all root-to-leaf paths. For example, given the following bi ...
- 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 = ...
- Python3解leetcode Same Tree
问题描述: Given two binary trees, write a function to check if they are the same or not. Two binary tree ...
- Python3解leetcode Symmetric Tree
问题描述: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). ...
随机推荐
- Only variables should be passed by reference
报错位置代码: $status->type = array_pop(explode('\\',$status->type)) (此处$status->type值原本是 APP\ ...
- 622FThe Sum of the k-th Powers
题目大意 求$\sum_{i=1}^{n} i^k$ 分析 我们发现这是一个$k+1$次多项式 因此我们求出前$k+2$项然后插值即可 由于$x_i = i$ 因此公式里面的乘机可以通过预处理然后循环 ...
- 杂项-Unicode:Unicode
ylbtech-杂项-Unicode:Unicode Unicode(统一码.万国码.单一码)是计算机科学领域里的一项业界标准,包括字符集.编码方案等.Unicode 是为了解决传统的字符编码方案的局 ...
- 关于public private protected访问修饰符
这个似乎都是老生常谈了,特别是找工作第一轮笔试的时候很爱考这些,再罗列一次,特别要注意继承的情况: 默认状态:即是不加修饰符的时候,所谓的default状态,在类内部可以被访问,在相同的包下面 ...
- spring-第十三篇之零配置支持
1.搜索bean类,使用注解标注spring bean. @Component:标注一个普通的spring bean类 @Controller:标注一个控制器组件类(Java EE组件) @Servi ...
- hdu1394 Minimum Inversion Number (线段树求逆序数&&思维)
题目传送门 Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- C# 跨线程调用控件的4中方法
原文:C# 跨线程调用控件 在C# 的应用程序开发中, 我们经常要把UI线程和工作线程分开,防止界面停止响应. 同时我们又需要在工作线程中更新UI界面上的控件, 下面介绍几种常用的方法 阅读目录 线 ...
- 暂时放弃ts版个人博客转js版博客
我本打算信心满满的做个vue+ts做个博客的,其实架构搭的差不多了,但是我在用vuex的时候发现一个自己无法忍受的瑕疵,那就是在用vuex的时候,得利于普通版vuex的map语法糖实在太好用,这把我惯 ...
- 在win7下面清除samba用户的登录状态
相信会有一部分刚开始测试samba服务器的人会有过这样的疑惑? 在win7下面使用一个samba用户的username和passwd登录过后,之后每次进去都是以这样的username和passwd进去 ...
- 【JAVA】 04-Java中的多线程
链接: 笔记目录:毕向东Java基础视频教程-笔记 GitHub库:JavaBXD33 目录: <> <> 内容待整理: 多线程引入 概述 多线程: 进程:正在执行中的程序,其 ...