[LeetCode&Python] Problem 404. Sum of Left Leaves
Find the sum of all left leaves in a given binary tree.
Example:
3
/ \
9 20
/ \
15 7 There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24.
# 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 sumOfLeftLeaves(self, root):
"""
:type root: TreeNode
:rtype: int
"""
# Recursion method
'''
def rer(node,left):
if not node:
return 0
if left:
if node.left or node.right:
return rer(node.left,True)+rer(node.right,False)
else:
return node.val
else:
return rer(node.right,False)+rer(node.left,True) return rer(root,False)
'''
# Non-recursion method
ans=0
if root:
stack=[root] while stack:
node=stack.pop()
if node.left:
if not node.left.left and not node.left.right:
ans+=node.left.val
else:
stack.append(node.left)
if node.right:
stack.append(node.right)
return ans
[LeetCode&Python] Problem 404. Sum of Left Leaves的更多相关文章
- [LeetCode&Python] Problem 371. Sum of Two Integers
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...
- 【Leetcode】404. Sum of Left Leaves
404. Sum of Left Leaves [题目]中文版 英文版 /** * Definition for a binary tree node. * struct TreeNode { * ...
- 【LeetCode】404. Sum of Left Leaves 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目大意 题目大意 解题方法 递归 迭代 日期 [LeetCode] 题目地址:h ...
- LeetCode 404. Sum of Left Leaves (左子叶之和)
Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...
- LeetCode - 404. Sum of Left Leaves
Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...
- 16. leetcode 404. Sum of Left Leaves
Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 ...
- LeetCode 404. Sum of Left Leaves (C++)
题目: Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are t ...
- LeetCode之404. Sum of Left Leaves
------------------------------------------------------------------- 分两种情况: 1.当前节点拥有左孩子并且左孩子是叶子节点:左孩子 ...
- [LeetCode&Python] Problem 599. Minimum Index Sum of Two Lists
Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of favorite ...
随机推荐
- 记录一下ES6扩展运算符(三点运算符)...的用法
...运算符用于操作数组,有两种层面 1. 第一个叫做 展开运算符(spread operator),作用是和字面意思一样,就是把东西展开.可以用在array和object上都行. 比如: let a ...
- SourceInsight 4重启之后文件变只读无法编辑
SourceInsight4.0在导入代码后,用起来没问题,第二天,再开启sourceInsight,结果所有文件变成只读了,不能编辑,标签前面也有了叹号. 百度一下,有人说是版本控制的问题,但是sv ...
- astah-professional-7_2_0安装
astah-professional-7_2_0-1安装 1● 下载文件 2● 安装 replace D:\Program\astahprofessional\inst\astah-p ...
- java string字符拼接符"+"的研究
程序: public class Test { public static void main(String args[]) { String s1 = "abc"; String ...
- 离线安装docker镜像
假如由于网络原因,需要在一台无网络的电脑上运行镜像,那么docker是支持的. 你需要做的主要有3步骤: 1:先从一个有网络的电脑下载docker镜像 sudo docker pull ubuntu ...
- asp.net MVC之Result过滤器浅析
在asp.net MVC中,每一个Action方法完成之后都会返回一个结果,而我们可以在Result过滤器中根据需要修改这个结果.例如可以根据UserAgent来判断客户端的来源是手机还是PC端,从而 ...
- php随手记
引用(&)是变量的别名,而不是指针,可用unset(变量名)把此变量的别名注销掉,等于没有声明此变量. @为错误抑制符,可以用在任何表达式前面. ``为命令操作符,可以执行系统命令. inst ...
- PE文件 01 导入表
0x01 导入表结构 数据目录表中的第二个成员标记了导入表的RVA和Size大小,由此可以定位到导入表: typedef struct _IMAGE_DATA_DIRECTORY { DWORD ...
- Java语法基础学习DaySeven
---恢复内容开始--- 一.包装类——Wrapper 1.定义:针对八种基本数据类型定义相应的引用类型——包装类(封装类) boolean——Boolean byte——Byte ...
- zepto和jquery关于获取css样式的试用差别
例如 获取 html标签的 字体大小, zepto中方法:$("html").css( "font-size" ); jquery中方法:$("htm ...