作者: 负雪明烛
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. R 语言实战-Part 5-1笔记

    R 语言实战(第二版) part 5-1 技能拓展 ----------第19章 使用ggplot2进行高级绘图------------------------- #R的四种图形系统: #①base: ...

  2. 25-ZigZag Conversion

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  3. 8.Maximum Depth of Binary Tree

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

  4. MybatisPlus入门程序

    参考资料:MybatisPlus官网 环境搭建 创建数据库 CREATE DATABASE `mybatisplus` ​ USE `mybatisplus` ​ CREATE TABLE `user ...

  5. Ubuntu16.04安装 2.8.5版本Ansible

    wget https://bootstrap.pypa.io/pip/2.7/get-pip.py && python get-pip.py pip install --upgrade ...

  6. Echart显示后端mysql数据

    一.基本思想 1.将数据存储在mysql数据库中 2.后端链接数据库,将数据库中的数据保存为json格式 3.将json格式数据使用ajax传到前端JSP页面中的Echarts 二.实现的关键点 1. ...

  7. above, abrupt

    above 近义词: over, beyond, exceeding反义词: below, beneath, under, underneath 有从右往左写的文字,没有从下往上的.above-men ...

  8. npm下载错误解决办法

    解决办法:删除npmrc文件. 使用镜像 镜像使用方法(三种办法任意一种都能解决问题,建议使用第三种,将配置写死,下次用的时候配置还在):1.通过config命令12npm config set re ...

  9. Copy constructor vs assignment operator in C++

    Difficulty Level: Rookie Consider the following C++ program. 1 #include<iostream> 2 #include&l ...

  10. Linux学习 - 帮助命令

    一.获取帮助信息man(manual) 1 功能 获得命令或配置文件的帮助信息 2 语法 man  [1.5]  [命令或配置文件] 1 命令的帮助 (可用 whatis 代替) 5 配置文件的帮助 ...