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. php-fpm占用cpu和内存过高100% 解决办法

    参考网站: https://www.fujieace.com/php/php-fpm.html https://www.fujieace.com/php/pm-max_children-2.html ...

  2. xpath 获取表单的值

    <input type="hidden" id="hospital_id" value="6666sui"> $selector ...

  3. npm使用国内淘宝镜像的方法

    一.通过命令配置 1. 命令 npm config set registry https://registry.npm.taobao.org 2. 验证命令 npm config get regist ...

  4. nginx升级教程

    1.说明 CVE-2016-4450,可通过构造特定数据包,可引发nginx引用空指针,导致nginx出错从而造成拒绝服务攻击. 影响1.3.9到1.11.0的所有版本,进行修复的1.10.1和1.1 ...

  5. python 文件的打开与读取

    python 文件的打开与读取 其实网上其他人写的都挺好的,我也是看他们的.办公室用的2.7.笔记本用的3.6.发现没有file 类,尴尬了 with open(r'C:\Users\HBX\Docu ...

  6. paysoft 网关出现EntityRef:expecting;的错误。

    paysoft 网关出现EntityRef:expecting;的错误. 原因是传进去的url里有&字符,之前是自动转义的,现在要手工改成& http://www.runoob.com ...

  7. SpringBoot启动器

    pom.xml文件1.父项目 <parent> <groupId>org.springframework.boot</groupId> <artifactId ...

  8. learning scala read from file

    scala读文件:   example: scala> import scala.io.Sourceimport scala.io.Source scala> var inputFile ...

  9. Java 实现倒计时(由秒计算天、小时、分钟、秒)

    public class Countdown4 { private static long day = 0; private static long hour = 0; private static ...

  10. 愛拼才會贏--IPA--闽南语

    闽南语经典曲目.