Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf3208c8

Browse files
author
zongyanqi
committed
add Easy_404_Sum_of_Left_Leaves
1 parent6ed7ce9 commitf3208c8

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

‎Easy_404_Sum_of_Left_Leaves.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Find the sum of all left leaves in a given binary tree.
3+
4+
Example:
5+
6+
3
7+
/ \
8+
9 20
9+
/ \
10+
15 7
11+
12+
There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24.
13+
*
14+
*/
15+
16+
/**
17+
* Definition for a binary tree node.
18+
* function TreeNode(val) {
19+
* this.val = val;
20+
* this.left = this.right = null;
21+
* }
22+
*/
23+
24+
/**
25+
*@param {TreeNode} root
26+
*@return {number}
27+
*/
28+
varsumOfLeftLeaves=function(root){
29+
returnfn(root,false);
30+
};
31+
32+
functionfn(node,isLeft){
33+
if(!node)return0;
34+
if(!node.left&&!node.right){
35+
returnisLeft ?node.val :0;
36+
}
37+
38+
returnfn(node.left,true)+fn(node.right,false);
39+
40+
}
41+

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp