[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 ...
随机推荐
- JSON 对象 与 字符串 互转
$sui = [ 'xixixi' => 'suisuisui', 'hahaha' => 'longlonglong', ]; $data = json_encode($sui); pr ...
- FastDFS安装教程
1.下载 FastDFS下载:https://codeload.github.com/happyfish100/fastdfs/zip/master 库文件下载:https://codeload.gi ...
- 把旧系统迁移到.Net Core 2.0 日记(1) - Startup.cs 解析
因为自己到开发电脑转到Mac Air,之前的Webform/MVC应用在Mac 跑不起来,而且.Net Core 2.0 已经比较稳定了. 1. 为什么会有跨平台的.Net Core 近年来,我们已 ...
- pre强制 自动换行
转自:http://www.16sucai.com/2010/10/941.html <pre> 元素可定义预格式化的文本.被包围在 pre 元素中的文本通常会保留空格和换行符.而文本也会 ...
- 【Java算法】获得一个随机字符串
package suanfa; import java.util.Random; public class RandomStr { public static String getRandomStr( ...
- 内联函数inline的用法
一.什么是内联函数 在C语言中,如果一些函数被频繁调用,不断地有函数入栈,即函数栈,会造成栈空间或栈内存的大量消耗.为了解决这个问题,特别的引入了inline修饰符,表示为内联函数. 栈空间就是指放 ...
- Vue + Element UI 实现权限管理系统(第三方图标库)
使用第三方图标库 用过Elment的同鞋都知道,Element UI提供的字体图符少之又少,实在是不够用啊,幸好现在有不少丰富的第三方图标库可用,引入也不会很麻烦. Font Awesome Font ...
- python-第一类对象,闭包,迭代器
# def fn(): # print("我叫fn") # fn() # print(fn) # <function fn at 0x0000000001D12E18> ...
- Model1与Model2
Model1与Model2开发模式的介绍及区别 转载 浅析Java开发中的Model1和Model2
- java Calendar类得到每个月的周末是几号的工具方法
public static List getWeekendInMonth(int year, int month) { List list = new ArrayList(); Calendar ca ...