添加新题目

This commit is contained in:
2025-08-26 06:18:10 +08:00
parent e1546166a3
commit 72f4dca9ed
18 changed files with 485 additions and 0 deletions

View File

@@ -0,0 +1,27 @@
package main
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func main() {
}
func kthSmallest(root *TreeNode, k int) int {
var helper func(root *TreeNode) []int
helper = func(root *TreeNode) []int {
if root == nil {
return []int{}
}
tmp := make([]int, 0)
tmp = append(tmp, helper(root.Left)...)
tmp = append(tmp, root.Val)
tmp = append(tmp, helper(root.Right)...)
return tmp
}
res := helper(root)
return res[k-1]
}

View File

@@ -0,0 +1,41 @@
package main
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func main() {
}
func flatten(root *TreeNode) {
var helper func(root *TreeNode) []int
helper = func(root *TreeNode) []int {
if root == nil {
return []int{}
}
tmp := make([]int, 0)
tmp = append(tmp, root.Val)
tmp = append(tmp, helper(root.Left)...)
tmp = append(tmp, helper(root.Right)...)
return tmp
}
res := helper(root)
cur := root
for i := 0; i < len(res); i++ {
if i == 0 {
cur.Left = nil
continue
}
cur.Left = nil
tmpNode := &TreeNode{
Val: res[i],
Left: nil,
Right: nil,
}
cur.Right = tmpNode
cur = tmpNode
}
}

View File

@@ -0,0 +1,24 @@
package main
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func main() {
}
func inorderTraversal(root *TreeNode) []int {
if root == nil {
return []int{}
}
var res []int
leftnode := inorderTraversal(root.Left)
rightnode := inorderTraversal(root.Right)
res = append(res, leftnode...)
res = append(res, root.Val)
res = append(res, rightnode...)
return res
}

View File

@@ -0,0 +1,36 @@
package main
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func main() {
}
func rightSideView(root *TreeNode) []int {
if root == nil {
return []int{}
}
queue := []*TreeNode{root}
var res []int
for len(queue) > 0 {
length := len(queue)
var thelo int
for i := 0; i < length; i++ {
node := queue[0]
thelo = node.Val
queue = queue[1:]
if node.Left != nil {
queue = append(queue, node.Left)
}
if node.Right != nil {
queue = append(queue, node.Right)
}
}
res = append(res, thelo)
}
return res
}

View File

@@ -0,0 +1,37 @@
package main
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func main() {
}
func levelOrder(root *TreeNode) [][]int {
if root == nil {
return [][]int{}
}
queue := []*TreeNode{root}
var result [][]int
for len(queue) > 0 {
length := len(queue)
cur := make([]int, length)
for i := 0; i < length; i++ {
node := queue[0]
queue = queue[1:]
cur[i] = node.Val
if node.Left != nil {
queue = append(queue, node.Left)
}
if node.Right != nil {
queue = append(queue, node.Right)
}
}
result = append(result, cur)
}
return result
}

View File

@@ -0,0 +1,19 @@
package main
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func main() {}
func maxDepth(root *TreeNode) int {
if root == nil {
return 0
}
maxnum := 1
maxnum += max(maxDepth(root.Left), maxDepth(root.Right))
return maxnum
}

View File

@@ -0,0 +1,29 @@
package main
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func main() {
}
func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
if root == nil || root == p || root == q {
return root
}
left := lowestCommonAncestor(root.Left, p, q)
right := lowestCommonAncestor(root.Right, p, q)
if left != nil && right != nil {
return root
}
if left != nil {
return left
}
return right
}

View File

@@ -0,0 +1,28 @@
package main
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func main() {
}
func diameterOfBinaryTree(root *TreeNode) int {
a, b := maxpath(root.Right)
c, d := maxpath(root.Left)
return max(a+b, c, d)
}
func maxpath(p *TreeNode) (int, int) {
if p == nil {
return 0, 0
}
leftpathnum, leftcurmax := maxpath(p.Left)
rightpathnum, rightcurmax := maxpath(p.Right)
curmax := max(leftpathnum+rightpathnum, leftcurmax, rightcurmax)
return 1 + max(leftpathnum+rightpathnum), curmax
}

View File

@@ -0,0 +1,46 @@
package main
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func main() {
}
func buildTree(preorder []int, inorder []int) *TreeNode {
var builder func(p []int, in []int) *TreeNode
builder = func(p []int, in []int) *TreeNode {
if len(in) == 0 || len(p) == 0 {
return nil
}
rootnum := p[0]
value := -1
for i := 0; i < len(in); i++ {
if in[i] == rootnum {
value = i
}
}
var left []int
var right []int
if value != -1 {
left = in[:value]
right = in[value+1:]
} else {
return nil
}
tmp := &TreeNode{
Val: rootnum,
Left: builder(p[1:1+len(left)], left),
Right: builder(p[1+len(left):], right),
}
return tmp
}
res := builder(preorder, inorder)
return res
}

View File

@@ -0,0 +1,26 @@
package main
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func main() {
}
func isSymmetric(root *TreeNode) bool {
return checkifq(root.Left, root.Right)
}
func checkifq(a, b *TreeNode) bool {
if a == nil && b == nil {
return true
}
if a == nil || b == nil {
return false
}
return a.Val == b.Val && checkifq(a.Left, b.Right) && checkifq(a.Right, b.Left)
}

View File

@@ -0,0 +1,24 @@
package main
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func main() {
}
func sortedArrayToBST(nums []int) *TreeNode {
if len(nums) == 0 {
return nil
}
mid := len(nums) / 2
root := &TreeNode{Val: nums[mid]}
first := root
root.Left = sortedArrayToBST(nums[:mid])
root.Right = sortedArrayToBST(nums[mid+1:])
return first
}

View File

@@ -0,0 +1,20 @@
package main
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func main() {
}
func invertTree(root *TreeNode) *TreeNode {
if root == nil {
return nil
}
root.Left, root.Right = invertTree(root.Left), invertTree(root.Right)
root.Left, root.Right = root.Right, root.Left
return root
}

View File

@@ -0,0 +1,41 @@
package main
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func main() {
}
func pathSum(root *TreeNode, targetSum int) int {
if root == nil {
return 0
}
if root.Left == nil && root.Right == nil && root.Val == targetSum {
return 1
}
hash := make(map[int]int)
var culcalute func(root *TreeNode, hash map[int]int, total int, targetSum int) int
culcalute = func(roottmp *TreeNode, hash map[int]int, total int, targetSum int) int {
if roottmp == nil {
return 0
}
restmp := 0
hashtmp := make(map[int]int)
for k, v := range hash {
hashtmp[k] = v
}
totalnum := total + roottmp.Val
if value, ok := hash[totalnum-targetSum]; ok {
restmp = restmp + value
}
hashtmp[totalnum]++
res := culcalute(roottmp.Left, hashtmp, totalnum, targetSum) + culcalute(roottmp.Right, hashtmp, totalnum, targetSum) + restmp
return res
}
hash[0]++
result := culcalute(root, hash, 0, targetSum)
return result
}

View File

@@ -0,0 +1,36 @@
package main
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func main() {
}
func isValidBST(root *TreeNode) bool {
var helper func(root *TreeNode) []int
helper = func(root *TreeNode) []int {
if root == nil {
return []int{}
}
tmp := make([]int, 0)
tmp = append(tmp, helper(root.Left)...)
tmp = append(tmp, root.Val)
tmp = append(tmp, helper(root.Right)...)
return tmp
}
res := helper(root)
for i := 0; i < len(res); i++ {
if i == 0 {
continue
}
if res[i-1] >= res[i] {
return false
}
}
return true
}

View File

@@ -0,0 +1,9 @@
package main
func main() {
}
func numIslands(grid [][]byte) int {
return 0
}

View File

@@ -0,0 +1,15 @@
package main
type ListNode struct {
Val int
Next *ListNode
}
func main() {
}
func reverseKGroup(head *ListNode, k int) *ListNode {
return nil
}

14
链表/LRU缓存/main.go Normal file
View File

@@ -0,0 +1,14 @@
package main
type ListNode struct {
Val int
Next *ListNode
}
func main() {
}
func reverseKGroup(head *ListNode, k int) *ListNode {
return nil
}

View File

@@ -0,0 +1,13 @@
package main
type ListNode struct {
Val int
Next *ListNode
}
func main() {
}
func sortList(head *ListNode) *ListNode {
return nil
}