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

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

  2. 【Leetcode】404. Sum of Left Leaves

    404. Sum of Left Leaves [题目]中文版  英文版 /** * Definition for a binary tree node. * struct TreeNode { * ...

  3. 【LeetCode】404. Sum of Left Leaves 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目大意 题目大意 解题方法 递归 迭代 日期 [LeetCode] 题目地址:h ...

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

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

  6. 16. leetcode 404. Sum of Left Leaves

    Find the sum of all left leaves in a given binary tree. Example:     3    / \   9  20     /  \    15 ...

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

  8. LeetCode之404. Sum of Left Leaves

    ------------------------------------------------------------------- 分两种情况: 1.当前节点拥有左孩子并且左孩子是叶子节点:左孩子 ...

  9. [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 ...

随机推荐

  1. FTP服务器搭建(Centos7)

    1.1.1 查看是否安装vsftp rpm -qa | grep vsftpd 1.1.2 如果没有安装: yum -y install vsftpd 1.2.3 vsftpd.conf 配置文件 匿 ...

  2. dvwa安装、配置、使用教程(Linux)

    一.搭建LAMP环境 首先搭建好LAMP环境,如没配好参见“Linux+Apache+MySQL+PHP配置教程” 或者使用官方推荐的XAMPP:https://www.apachefriends.o ...

  3. js重写系统的弹框

     //调用系统的弹框,不显示地址 window.alert = function(name){    var iframe = document.createElement("IFRAME& ...

  4. shell 多重条件判断

    多重条件判断 '判断1 -a 判断2' 逻辑与,判断1和判断2都成立,最终的结果才为真 '判断1 -o 判断2' 逻辑或,判断1和判断2有一个成立,最终的结果就为真 '!判断' 逻辑非,使原始的判断式 ...

  5. 【oracle】oracle REGEXP_SUBSTR分割字符串

    REGEXP_SUBSTR的使用:通过REGEXP_SUBSTR进行数据分割: , LEVEL, 'i') AS STR FROM DUAL CONNECT BY LEVEL <= LENGTH ...

  6. mysql 下载资源地址

    http://mirror.neu.edu.cn/mysql/Downloads/MySQL-5.6/

  7. Asp.Net 中 HTTP 和 HTTPS 切换

    Asp.Net 中 HTTP 和 HTTPS 切换   目的 HTTP,超文本传输协议,明文传输,无状态,服务器默认端口80 HTTPS,具有SSL加密的HTTP,加密传输,需要申请ca证书,服务器默 ...

  8. OO第一阶段总(休)结(养)分(生)析(息)

    第一次作业: 这是一次让我认识到ddl面前潜力真的可以无限大的作业. 一直以为OO是一门和数据结构一样先用几周的时间讲讲Java然后写写”Hello World”小程序再开始讲正课的我(我也不知道为什 ...

  9. 浅谈caffe中train_val.prototxt和deploy.prototxt文件的区别

    本文以CaffeNet为例: 1. train_val.prototxt  首先,train_val.prototxt文件是网络配置文件.该文件是在训练的时候用的. 2.deploy.prototxt ...

  10. 每天CSS学习之letter-spacing

    letter-spacing是CSS的一个属性,其作用是设置字符之间的距离.letter意为字符. 1.normal:规定字符之间没有额外的空间.该值是默认值.如下示例: p{ letter-spac ...