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). ...
随机推荐
- How-To-Ask-Questions-The-Smart-Way提问的技巧 提问的智慧
How-To-Ask-Questions-The-Smart-Way https://github.com/ryanhanwu/How-To-Ask-Questions-The-Smart-Way/b ...
- Bulk synchronous parallel
https://en.wikipedia.org/wiki/Bulk_synchronous_parallel https://zh.wikipedia.org/wiki/整体同步计算模型 超级步(S ...
- python读写excle
我们可以通过python的一些模块进行excle中用例的读取,或者导出数据到excle 目录 1.安装模块 2.读excle 3.写excle 1.安装模块 python中有第三方模块可以进行excl ...
- TCP概述
1. TCP提供的服务 我们知道TCP是一个面向连接.提供可靠数据数据传输服务的传输层协议.面向连接意味着发送端和接收端在交换数据前需要建立一个连接,和我们平常打电话一样,在通话前,需要拨号建立连接. ...
- 使用python执行sql语句和外键解析
一.下载并导入pymysql pip install pymysql && import pymysql db=pymysql.connect(host=) #如果报错host大概率因 ...
- Linux运维工程师前景
什么是Linux运维 如果我们是一辆高速行驶在高速公路上的汽车,那运维工程师就是司机兼维修工,这个司机可不简单,有时需要在高速行驶过程中更换轮胎.并根据道路情况换档位.当汽车速度越来越快时,汽车本身不 ...
- Python 操作 mongodb 亿级数据量使用 Bloomfilter 高效率判断唯一性 例子
工作需要使用 python 处理 mongodb 数据库两亿数据量去重复,需要在大数据量下快速判断数据是否存在 参考资料:https://segmentfault.com/q/101000000061 ...
- Xpath表达式的粗介绍
关于在自动化中Xpath表达式的书写,其实我也只是刚刚入门,粗略的跟着网上的教程学了一下,这篇我就来分享总结一下我学习到的知识. 首先呢,我们先认识一下什么是Xpath.Xpath是XML路径语言,它 ...
- HDU 6583 Typewriter 题解
——本题来自杭电多校第一场 题意:给定一个字符串,主角需要用打字机将字符串打出来,每次可以: 1.花费p来打出任意一个字符 2.花费q来将已经打出的某一段(子串)复制到后面去 对于这种最优化的问题,我 ...
- 排序算法一:插入排序(Insertion sort)
最近从网易公开课在看麻省理工学院的公开课<算法导论>,感觉还不错,接下来几篇文章所示学习日记了,不准备对算法细节做过多描述,感兴趣的可以自己去看. 文章分几篇讲经典排序算法,直接上代码,根 ...