作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


[LeetCode]

题目地址:https://leetcode.com/problems/sum-of-left-leaves/

  • Difficulty: Easy

题目大意

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.

题目大意

求一个二叉树中所有的左叶子节点的和。

解题方法

递归

Java解法是这样的。

这个方法很直白很简单,用递归判断是不是左叶子,然后求和即可。我的第一次A过的代码是这样的。

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
int sum=0;
public int sumOfLeftLeaves(TreeNode root) {
if(root==null){
return 0;
}
if(root.left!=null){
if(root.left.left==null && root.left.right==null){//根的左边节点是叶子
sum += root.left.val;//加上左叶子的值
}
sumOfLeftLeaves(root.left);//循环左叶子
}
if(root.right!=null){
sumOfLeftLeaves(root.right);//循环右叶子
} return sum;
}
}

AC:8 ms

看了高票解答之后,感觉自己还可以精简下代码。如下。

public class Solution {
public int sumOfLeftLeaves(TreeNode root) {
if(root==null){
return 0;
}
int sum=0;
if(root.left!=null){
if(root.left.left==null && root.left.right==null){
sum += root.left.val;
}else{
sum += sumOfLeftLeaves(root.left);
}
} sum += sumOfLeftLeaves(root.right); return sum;
}
}

AC:9 ms

Python解法是这样的。

# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution:
def sumOfLeftLeaves(self, root):
"""
:type root: TreeNode
:rtype: int
"""
if not root: return 0
self.sum = 0
self.inOrder(root)
return self.sum def inOrder(self, root):
if not root: return
if root.left:
self.inOrder(root.left)
if not root.left.left and not root.left.right:
self.sum += root.left.val
if root.right:
self.inOrder(root.right)

迭代

对于树的问题,一般都可以使用递归和迭代两种解法。这个题用迭代的话,需要用栈,总体代码和递归基本一样的。

Python解法如下:

# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution:
def sumOfLeftLeaves(self, root):
"""
:type root: TreeNode
:rtype: int
"""
if not root: return 0
stack = []
stack.append(root)
leftsum = 0
while stack:
node = stack.pop()
if not node: continue
if node.left:
if not node.left.left and not node.left.right:
leftsum += node.left.val
stack.append(node.left)
if node.right:
stack.append(node.right)
return leftsum

日期

2017 年 1 月 7 日
2018 年 11 月 14 日 —— 很严重的雾霾

【LeetCode】404. Sum of Left Leaves 解题报告(Python)的更多相关文章

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

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

  3. 16. leetcode 404. Sum of Left Leaves

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

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

  5. [leetcode]404. Sum of Left Leaves左叶子之和

    弄个flag记录是不是左节点就行 int res = 0; public int sumOfLeftLeaves(TreeNode root) { if (root==null) return res ...

  6. 【LeetCode】784. Letter Case Permutation 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 循环 日期 题目地址:https://leet ...

  7. 【LeetCode】206. Reverse Linked List 解题报告(Python&C++&java)

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

  8. 【LeetCode】654. Maximum Binary Tree 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...

  9. 【Leetcode】404. Sum of Left Leaves

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

随机推荐

  1. 34. Swap Nodes in Pairs

    Swap Nodes in Pairs My Submissions QuestionEditorial Solution Total Accepted: 95230 Total Submission ...

  2. IDEA+maven+javafx(java 1.8)入坑记录

    序 好久没写博客了,主要是因为懒,写博客真的是个难坚持的事.但今天登上来看了看,之前记录ctf写的wp竟然点击量这么多了,突然让我有了继续写下去的动力. 这段时间遇到了好多事,中间也有想过写几篇文章记 ...

  3. 使用Mybatis出现的问题+配置优化+ResultMap

    一.可能出现的问题 1.Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: ...

  4. 记一次 .NET 某化妆品 webapi 卡死分析

    一:背景 1. 讲故事 10月份星球里的一位老朋友找到我,说他们公司的程序在一个网红直播带货下给弄得无响应了,无响应期间有大量的 RabbitMQ 超时,寻求如何找到根源,聊天截图我就不发了. 既然无 ...

  5. java运行报错 has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0

    解决方法: 解决办法: 在项目的属性里设置jdk版本,方法是右击项目-->properties-->java compiler --> Enable project specific ...

  6. 转Android service 启动篇之 startForegroundService

    本文转自:https://blog.csdn.net/shift_wwx/article/details/82496447 前言: 在官方文档 Android 8.0 行为变更 中有这样一段话: An ...

  7. NSMutableArray-->NSString

    1.如何把NSMutableArray 转化为NSString//用字符将NSArray中的元素拼接起来 NSArray *array = [NSArray arrayWithObjects:@&qu ...

  8. OC-私有方法,构造方法,类的本质及启动过程

    总结 标号 主题 内容 一 OC的私有方法 私有变量/私有方法 二 @property 概念/基本使用/寻找方法的过程/查找顺序 三 @synthesize @synthesize概念/基本使用/注意 ...

  9. When should we write our own copy constructor?

    Reference:http://www.fredosaurus.com/notes-cpp/oop-condestructors/copyconstructors.html Please write ...

  10. 【Linux】【Services】【Disks】bftfs

    1. 简介 1.1 Btrfs(B-tree,Butter FS,Better FS) 1.2. 遵循GPL,由oracle在2007年研发,支持CoW 1.3. 主要为了替代早期的ext3/ext4 ...