添加新题目
This commit is contained in:
27
二叉树/二叉搜索树中第K小的元素/main.go
Normal file
27
二叉树/二叉搜索树中第K小的元素/main.go
Normal 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]
|
||||||
|
}
|
||||||
41
二叉树/二叉树展开为链表/main.go
Normal file
41
二叉树/二叉树展开为链表/main.go
Normal 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
|
||||||
|
}
|
||||||
|
}
|
||||||
24
二叉树/二叉树的中序遍历/main.go
Normal file
24
二叉树/二叉树的中序遍历/main.go
Normal 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
|
||||||
|
}
|
||||||
36
二叉树/二叉树的右视图/main.go
Normal file
36
二叉树/二叉树的右视图/main.go
Normal 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
|
||||||
|
}
|
||||||
37
二叉树/二叉树的层序遍历/main.go
Normal file
37
二叉树/二叉树的层序遍历/main.go
Normal 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
|
||||||
|
}
|
||||||
19
二叉树/二叉树的最大深度/main.go
Normal file
19
二叉树/二叉树的最大深度/main.go
Normal 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
|
||||||
|
}
|
||||||
29
二叉树/二叉树的最近公共祖先/main.go
Normal file
29
二叉树/二叉树的最近公共祖先/main.go
Normal 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
|
||||||
|
|
||||||
|
}
|
||||||
28
二叉树/二叉树的直径/main.go
Normal file
28
二叉树/二叉树的直径/main.go
Normal 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
|
||||||
|
}
|
||||||
46
二叉树/从前序与中序遍历序列构造二叉树/main.go
Normal file
46
二叉树/从前序与中序遍历序列构造二叉树/main.go
Normal 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
|
||||||
|
}
|
||||||
26
二叉树/对称二叉树/main.go
Normal file
26
二叉树/对称二叉树/main.go
Normal 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)
|
||||||
|
}
|
||||||
24
二叉树/将有序数组转换为二叉搜索树/main.go
Normal file
24
二叉树/将有序数组转换为二叉搜索树/main.go
Normal 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
|
||||||
|
}
|
||||||
20
二叉树/翻转二叉树/main.go
Normal file
20
二叉树/翻转二叉树/main.go
Normal 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
|
||||||
|
}
|
||||||
41
二叉树/路径总和III/main.go
Normal file
41
二叉树/路径总和III/main.go
Normal 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
|
||||||
|
}
|
||||||
36
二叉树/验证二叉搜索树/main.go
Normal file
36
二叉树/验证二叉搜索树/main.go
Normal 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
|
||||||
|
}
|
||||||
9
图论/岛屿数量/main.go
Normal file
9
图论/岛屿数量/main.go
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func numIslands(grid [][]byte) int {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
15
链表/K个一组翻转链表/main.go
Normal file
15
链表/K个一组翻转链表/main.go
Normal 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
14
链表/LRU缓存/main.go
Normal 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
|
||||||
|
}
|
||||||
13
链表/排序链表/main.go
Normal file
13
链表/排序链表/main.go
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
type ListNode struct {
|
||||||
|
Val int
|
||||||
|
Next *ListNode
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
|
}
|
||||||
|
func sortList(head *ListNode) *ListNode {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user