We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 351ae08 commit c34f34fCopy full SHA for c34f34f
problems401-450/404.sum-of-left-leaves.go
@@ -0,0 +1,35 @@
1
+/* 404. 左叶子之和
2
+计算给定二叉树的所有左叶子之和。
3
+
4
+示例:
5
6
+ 3
7
+ / \
8
+ 9 20
9
10
+ 15 7
11
12
+在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24 */
13
+/**
14
+ * Definition for a binary tree node.
15
+ * type TreeNode struct {
16
+ * Val int
17
+ * Left *TreeNode
18
+ * Right *TreeNode
19
+ * }
20
+ */
21
+func sumOfLeftLeaves(root *TreeNode) int {
22
+ if root == nil || (root.Left == nil && root.Right == nil) {
23
+ return 0
24
+ }
25
+ if root.Left == nil {
26
+ return sumOfLeftLeaves(root.Right)
27
28
+ if root.Left.Left == nil && root.Left.Right == nil {
29
+ return root.Left.Val + sumOfLeftLeaves(root.Right)
30
31
+ if root.Right == nil {
32
+ return sumOfLeftLeaves(root.Left)
33
34
+ return sumOfLeftLeaves(root.Left) + sumOfLeftLeaves(root.Right)
35
+}
0 commit comments