leetcode 404 左叶子之和 Sum of Left Leaves

C++ 递归遍历+判断左叶子节点,效率不高,

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     int res;
13     int sumOfLeftLeaves(TreeNode* root) {
14         DFS(root);
15         return res;
16     }
17     void DFS(TreeNode* root){
18         if(root==NULL) return;
19         DFS(root->left);
20         if(root->left!=NULL&&root->left->left==NULL&&root->left->right==NULL)
21             res+=root->left->val;
22         DFS(root->right);
23     }
24 };

更多答案参考大佬解答:

https://leetcode.com/problems/sum-of-left-leaves/discuss/244628/six-ways-to-solve-this-question

明天好好学习一下

原文地址:https://www.cnblogs.com/joelwang/p/10441021.html

时间: 2024-11-10 18:13:37

leetcode 404 左叶子之和 Sum of Left Leaves的相关文章

[Swift]LeetCode404. 左叶子之和 | 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. 计算给定二叉树的所有左叶子之和. 示例: 3 / 9 20 / 15 7 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24 1 /**

LeetCode: 404.左叶子节点

计算给定二叉树的所有左叶子之和. 示例: 3 / \ 9 20 / \ 15 7 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24 解析 我们需要找到这样的节点 属于叶子节点 属于父节点的左子节点 方法一:用栈,dfs遍历,用全局变量res作为累积和.遍历的过程中传递该节点是否是左子节点.同时判断左右子节点是否为None,则可以知道是不是左叶子节点. class Solution: def sumOfLeftLeaves(self, root: TreeNode) -> int

404 Sum of Left Leaves 左叶子之和

计算给定二叉树的所有左叶子之和.示例:    3   / \  9  20    /  \   15   7在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24. 详见:https://leetcode.com/problems/sum-of-left-leaves/description/ C++: 方法一: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *le

【leetcode 简单】 第九十四题 左叶子之和

计算给定二叉树的所有左叶子之和. 示例: 3 / 9 20 / 15 7 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 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 sumOfLeft

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. 题目标签:Tree 这道题目给了我们一个二叉树,让我们找到所有左子叶之和.这里需要另外一个function - sumLeftLeaves 还要一个int

[LeetCode] 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. 这道题让我们求一棵二叉树的所有左子叶的和,那么看到这道题我们知道这肯定是考二叉树的遍历问题,那么最简洁的写法肯定是用递归,由于我们只需要累加左子叶之和,那

LeetCode 第一题,Two Sum

今天早上起来去机房的路上还在想,一直不做算法题老是觉得不踏实.做做题总是让自己觉得自己真的在做做学习.... 这也算是一种强迫症吧. 那就从今天开始做做LeetCode,因为好久没做过了,所以第一题还是看了别人的题解和思路,算是找找感觉. 总的来说第一题是个水.... 题目还原 Two Sum Given an array of integers, find two numbers such that they add up to a specific target number. The fu

LeetCode: Binary Tree Maximum Path Sum [124]

[题目] Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example: Given the below binary tree, 1 / 2 3 Return 6. [题意] 给定一棵二叉树,找出其中路径和最大的路径,然会返回最大路径和. 本题中的路径不是从根节点到叶子节点这样的传统的路径,而是指的二叉树中任意两个节点之间的联通路径.

404. Sum of Left Leaves (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. 题意:计算所有左叶子的和思路:遍历二叉树 1.检查当前节点的左子节点是否是左子叶:2.若是叶子节点,则返回左子叶的值加上对当前结点的右子节点调用递归的结果