404. Sum of Left Leaves#2(Done)

原创 2017年01月03日 16:27:24

Solution#2

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
 // 递归
public class Solution {
    public int sumOfLeftLeaves(TreeNode root) {
        if (root == null || (root.left == null && root.right == null)) return 0;
        int result = 0;
        if (root.left != null) {
            if (root.left.left == null && root.left.right == null)
                result += root.left.val;
            else {
                result += sumOfLeftLeaves(root.left);
            }
        }
        if (root.right != null) {
            result += sumOfLeftLeaves(root.right);
        }
        return result;
    }
}

Solution#1

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
 // 迭代
public class Solution {
    public int sumOfLeftLeaves(TreeNode root) {
        if (root == null || (root.left == null && root.right == null)) return 0;
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        int result = 0;
        while(stack.size() != 0) {
            TreeNode tmp = stack.pop();
            if (tmp.right != null) {
                stack.push(tmp.right);
            }
            if (tmp.left != null) {
                if (tmp.left.left == null && tmp.left.right == null) result += tmp.left.val;
                else stack.push(tmp.left);
            }
        }
        return result;
    }
}

Problem#2

  • 时间复杂度太高(26.2%)
  • 代码不够简洁,减少几个不必要的判断条件后,时间提升至(75.96%)

Problem#1

  • 时间复杂度太高(15.16%)
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

404. Sum of Left Leaves

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

leetcode(88).404. Sum of Left Leaves

题意: 给定一棵二叉树,返回所有左叶子值的和。 初步分析: 递归:除了节点以外还需要传递和的信息,后来还发现还需要传递判断是不是左结点的信息。 /** * Definition for a bina...

412. Fizz Buzz \ 404. Sum of Left Leaves

412法一:class Solution { public: vector fizzBuzz(int n) { vector result; for(int i ...

【LeetCode】404 Sum of Left Leaves(java实现)

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

LeetCode 404. Sum of Left Leaves

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

LeetCode 404. Sum of Left Leaves 解题报告

LeetCode 404. Sum of Left Leaves 解题报告

[LeetCode]404. Sum of Left Leaves(叶子结点的和)

404. Sum of Left LeavesFind the sum of all left leaves in a given binary tree. 题目大意: 找出二叉树所有左叶...

LeetCode之路:404. Sum of Left Leaves

LeetCode之路:404. Sum of Left Leaves 一、引言这是一道乍看比较简单,但是稍不注意做起来就会比较痛苦的题目 T_T ~~~这道题我做了很久,一开始审错了题目,提交了之后看...

LeetCode 404. Sum of Left Leaves 题解(C++)

LeetCode 404. Sum of Left Leaves 题解(C++)题目描述 Find the sum of all left leaves in a given binary tree....

【LeetCode】Sum of Left Leaves 左叶子之和

LeetCode 404. Sum of Left Leaves Find the sum of all left leaves in a given binary tree. 给定一棵二叉树,找出...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)