[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 left leaves in the binary tree, with values 9 and 15 respectively. Return 24.

我们递归dfs的时候,是求解的全部子树,但是如果我们弄个标志位,判断是不是左子树,那么那个递归一样可以用。

public int sumOfLeftLeaves(TreeNode root) {
        return dfs(root, false);
    }

    private int dfs(TreeNode root, boolean isLeaf) {
        if (root == null)
            return 0;
        if (root.left == null && root.right == null && isLeaf)
            return root.val;
        return dfs(root.left, true) + dfs(root.right, false);
    }

笨一点的办法。但是也能AC。

public int sumOfLeftLeaves1(TreeNode root) {
        if (root == null)
            return 0;
        else if (root.left == null && root.right == null)
            return 0;
        else {
            return ((root.left != null && root.left.left == null && root.left.right == null) ? root.left.val
                    : sumOfLeftLeaves(root.left))
                    + ((root.right != null && (root.right.left != null || root.right.right != null)) ? sumOfLeftLeaves(root.right)
                            : 0);
        }
    }
时间: 2024-09-20 11:47:22

[LeetCode]--404. Sum of Left Leaves的相关文章

[LeetCode] Path Sum IV 二叉树的路径和之四

If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digits integers. For each integer in this list: The hundreds digit represents the depth D of this node, 1 <= D <= 4. The tens digit represents the positio

[LeetCode] Map Sum Pairs 映射配对之和

Implement a MapSum class with insert, and sum methods. For the method insert, you'll be given a pair of (string, integer). The string represents the key and the integer represents the value. If the key already existed, then the original key-value pai

[LeetCode] Two Sum IV - Input is a BST 两数之和之四 - 输入是二叉搜索树

Given a Binary Search Tree and a target number, return true if there exist two elements in the BST such that their sum is equal to the given target. Example 1: Input: 5 / \ 3 6 / \ \ 2 4 7 Target = 9 Output: True Example 2: Input: 5 / \ 3 6 / \ \ 2 4

[LeetCode]129.Sum Root to Leaf Numbers

[题目] Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 /

[LeetCode]--371. Sum of Two Integers

Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Example: Given a = 1 and b = 2, return 3. Credits: Special thanks to @fujiaozhu for adding this problem and creating all test cases. 不用+号肯定想到用Java位运算 位运算中

[LeetCode] Range Sum Query - Immutable

Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. Example: Given nums = [-2, 0, 3, -5, 2, -1] sumRange(0, 2) -> 1 sumRange(2, 5) -> -1 sumRange(0, 5) -> -3 Note: You may assume that the array do

[LeetCode] Two Sum

Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that

LeetCode All in One 题目讲解汇总(持续更新中...)

终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通过OJ,或是有更好的解法,或是有任何疑问,意见和建议的话,请一定要在对应的帖子下面评论区留言告知博主啊,多谢多谢,祝大家刷得愉快,刷得精彩,刷出美好未来- 博主制作了一款iOS的应用"Leetcode Meet Me",里面有Leetcode上所有的题目,并且贴上了博主的解法,随时随地都能

[C/C++基础知识] 一篇就让你彻底搞懂qsort快速排序的文章

        最近在做LeetCode的题目.面试和笔试后发现经常考察快速排序的知识.通过这篇文章介绍,能让你彻底的了解和学习快排,主要从一下三个部分进行介绍:         一.C语言实现qsort快速排序         二.快速排序的原理及手写快排源码         三.LeetCode关于Two Sum的快排实现 参考文献:        <算法分析与设计>关于分治法那章内容         如何利用C语言中的qsort库函数实现快速排序 - by:stpeace