mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-04 16:12:47 +08:00
规范格式
This commit is contained in:
27
leetcode/0404.Sum-of-Left-Leaves/404. Sum of Left Leaves.go
Normal file
27
leetcode/0404.Sum-of-Left-Leaves/404. Sum of Left Leaves.go
Normal file
@ -0,0 +1,27 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
// TreeNode define
|
||||
type TreeNode = structures.TreeNode
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* type TreeNode struct {
|
||||
* Val int
|
||||
* Left *TreeNode
|
||||
* Right *TreeNode
|
||||
* }
|
||||
*/
|
||||
|
||||
func sumOfLeftLeaves(root *TreeNode) int {
|
||||
if root == nil {
|
||||
return 0
|
||||
}
|
||||
if root.Left != nil && root.Left.Left == nil && root.Left.Right == nil {
|
||||
return root.Left.Val + sumOfLeftLeaves(root.Right)
|
||||
}
|
||||
return sumOfLeftLeaves(root.Left) + sumOfLeftLeaves(root.Right)
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
type question404 struct {
|
||||
para404
|
||||
ans404
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para404 struct {
|
||||
one []int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans404 struct {
|
||||
one int
|
||||
}
|
||||
|
||||
func Test_Problem404(t *testing.T) {
|
||||
|
||||
qs := []question404{
|
||||
|
||||
question404{
|
||||
para404{[]int{}},
|
||||
ans404{0},
|
||||
},
|
||||
|
||||
question404{
|
||||
para404{[]int{3, 9, 20, structures.NULL, structures.NULL, 15, 7}},
|
||||
ans404{24},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 404------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans404, q.para404
|
||||
fmt.Printf("【input】:%v ", p)
|
||||
root := structures.Ints2TreeNode(p.one)
|
||||
fmt.Printf("【output】:%v \n", sumOfLeftLeaves(root))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
28
leetcode/0404.Sum-of-Left-Leaves/README.md
Executable file
28
leetcode/0404.Sum-of-Left-Leaves/README.md
Executable file
@ -0,0 +1,28 @@
|
||||
# [404. Sum of Left Leaves](https://leetcode.com/problems/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.
|
||||
|
||||
|
||||
## 题目大意
|
||||
|
||||
计算给定二叉树的所有左叶子之和。
|
||||
|
||||
|
||||
## 解题思路
|
||||
|
||||
|
||||
- 这一题是微软的面试题。递归求解即可
|
||||
|
Reference in New Issue
Block a user