Skip to content

Commit 1a1f161

Browse files
Forge YaoForge Yao
authored andcommitted
commit 112/118
1 parent 6cb1368 commit 1a1f161

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

112/112-hasPathSum.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// https://leetcode-cn.com/problems/path-sum/
2+
package main
3+
4+
import (
5+
"fmt"
6+
"leetcode/util"
7+
)
8+
9+
type TreeNode = util.TreeNode
10+
11+
func hasPathSum(root *TreeNode, targetSum int) bool {
12+
if root == nil {
13+
return false
14+
}
15+
if root.Left == nil && root.Right == nil {
16+
return root.Val == targetSum
17+
}
18+
19+
return (hasPathSum(root.Left, targetSum-root.Val) || hasPathSum(root.Right, targetSum-root.Val))
20+
}
21+
22+
func main() {
23+
n := [][]int{{5, 4, 8, 11, 0, 13, 4, 7, 2, 0, 0, 0, 1}, {1, 2, 3}, {1, 2}}
24+
target := []int{22, 5, 0}
25+
for i := 0; i < len(n) && i < len(target); i++ {
26+
fmt.Println("ret:", hasPathSum(util.CreateTree(n[i]), target[i]))
27+
}
28+
}

118/118-generate.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// https://leetcode-cn.com/problems/pascals-triangle/
2+
package main
3+
4+
import "fmt"
5+
6+
func generate(numRows int) [][]int {
7+
num := make([][]int, numRows)
8+
for i := 0; i < numRows; i++ {
9+
num[i] = make([]int, i+1)
10+
for j := 0; j <= i; j++ {
11+
if j == 0 || j == i {
12+
num[i][j] = 1
13+
} else {
14+
num[i][j] = num[i-1][j-1] + num[i-1][j]
15+
}
16+
}
17+
}
18+
return num
19+
}
20+
21+
func main() {
22+
n := []int{5, 7}
23+
for i := 0; i < len(n); i++ {
24+
fmt.Println("ret:", generate(n[i]))
25+
}
26+
}

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ LeetCode
3333
|108|[将有序数组转换为二叉搜索树](https://leetcode-cn.com/problems/convert-sorted-array-to-binary-search-tree/)|[Go](./108/108-sortedArrayToBST.go)|简单|二叉搜索树|
3434
|110|[平衡二叉树](https://leetcode-cn.com/problems/balanced-binary-tree/)|[Go](./110/110-isBalanced.go)|简单|二叉树|
3535
|111|[二叉树的最小深度](https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/)|[Go](./111/111-minDepth.go)|简单|二叉树|
36+
|112|[路径总和](https://leetcode-cn.com/problems/path-sum/)|[Go](./112/112-hasPathSum.go)|简单|二叉树|
37+
|118|[杨辉三角](https://leetcode-cn.com/problems/pascals-triangle/)|[Go](./118/118-generate.go)|简单||
3638
|125|[验证回文串](https://leetcode-cn.com/problems/valid-palindrome/)|[Go](./125-isPalindrome/125-isPalindrome.go)|简单||
3739
|131|[分割回文串](https://leetcode-cn.com/problems/palindrome-partitioning/) | [Go](./131-partition/131-partition.go)|中等|递归, 动态规划|
3840
|141|[环形链表](https://leetcode-cn.com/problems/linked-list-cycle/)|[Go](./141-hasCycle/141-hasCycle.go)|简单|双指针|

0 commit comments

Comments
 (0)