问题描述:

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的更多相关文章

  1. 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 ...

  2. Python3解leetcode Binary Tree PathsAdd Digits

    问题描述: Given a non-negative integer num, repeatedly add all its digits until the result has only one ...

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

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

  4. leetcode : Binary Tree Paths

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

  5. LeetCode——Binary Tree Paths

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

  6. LeetCode Binary Tree Paths(简单题)

    题意: 给出一个二叉树,输出根到所有叶子节点的路径. 思路: 直接DFS一次,只需要判断是否到达了叶子,是就收集答案. /** * Definition for a binary tree node. ...

  7. leetcode Binary Tree Paths python

    # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...

  8. Python3解leetcode Same Tree

    问题描述: Given two binary trees, write a function to check if they are the same or not. Two binary tree ...

  9. Python3解leetcode Symmetric Tree

    问题描述: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). ...

随机推荐

  1. Python专题三字符串的基础知识

    Python专题三字符串的基础知识 在Python中最重要的数据类型包括字符串.列表.元组和字典等.该篇主要讲述Python的字符串基础知识. 一.字符串基础 字符串指一有序的字符序列集合,用单引号. ...

  2. Kestrel web server implementation in ASP.NET Core

    https://docs.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel?tabs=aspnetcore1x&view ...

  3. virtualenv-windows下排坑

    1. 安装 pip install virtualenv pip install virtualenvwrapper-win    (win下一定要有这个 -win,不然后续 workon,mkvir ...

  4. EasyUI日期控件获值和赋值

    一,获值 1.$("#id").datebox('getValue') 2.$("input[name='mydate']").val() 参考:http:// ...

  5. 次小生成树(Prim + Kruaskal)

    问题引入: 我们先来回想一下生成树是如何定义的,生成树就是用n - 1条边将图中的所有n个顶点都连通为一个连通分量,这样的边连成子树称为生成树. 最小生成树很明显就是生成树中权值最小的生成树,那么我们 ...

  6. 2019牛客暑期多校训练营(第一场) - H - XOR - 线性基

    https://ac.nowcoder.com/acm/contest/881/H 题意: 给定n个整数,求其中异或和为 \(0\) 的子集的大小的和. 题解思路: 首先转化为每个可以通过异或表示 \ ...

  7. python学习第六天运算符总结大全

    python学习第六天运算符总结大全,玖乐网络(www.96net.com.cn)列出下面详细 1,算术运算符 + 加 - 减 *乘 / 除 % 求模 ** 乘方 // 取整除 - 返回商的整数部分 ...

  8. python 字符编码问题总结

    都是计算机存储是二进制0101之类的数字 最早计算机在美国开始的 所以数字和英文之类的占用八位 2的8次方 256可以存储对于英文和数字戳戳有余  每个国家都有自己的编码 中国 gb2312 gbk ...

  9. React中异步模块api React.lazy和React.Suspense

    React.lazy React.lazy 这个函数需要动态调用 import().它必须返回一个 Promise,该 Promise 需要 resolve 一个 defalut export 的 R ...

  10. tensorflow用dropout解决over fitting

    在机器学习中可能会存在过拟合的问题,表现为在训练集上表现很好,但在测试集中表现不如训练集中的那么好. 图中黑色曲线是正常模型,绿色曲线就是overfitting模型.尽管绿色曲线很精确的区分了所有的训 ...