递归

48 阅读2分钟

70. 爬楼梯

func climbStairs(n int) int {
	if n < 4 {
		return n
	}
	a, b, c := 1, 2, 3
	for i := 4; i <= n; i++ {
		a, b, c = b, a+b, b+c
	}
	return c
}

22. 括号生成

func generateParenthesis(n int) []string {
    ans := []string{}
    helper(n, 0, 0, "", &ans)
    return ans
}

func helper(n, left, right int, path string, ans *[]string) {
    if right > left || left > n {
        return
    }
    if left == n && left == right {
        *ans = append(*ans, path)
    }

    helper(n, left+1, right, path+"(", ans)
    helper(n, left, right+1, path+")", ans)
}

226. 翻转二叉树

func invertTree(root *TreeNode) *TreeNode {
    if root == nil {
        return root
    }

    root.Left = invertTree(root.Left)
    root.Right = invertTree(root.Right)

    root.Left, root.Right = root.Right, root.Left

    return root
}

98. 验证二叉搜索树

func isValidBST(root *TreeNode) bool {
    return helper(root, math.MinInt64, math.MaxInt64)
}

func helper(node *TreeNode, min, max int) bool {
    if node == nil {
        return true
    }
    if node.Val <= min || node.Val >= max {
        return false
    }
    return helper(node.Left, min, node.Val) && helper(node.Right, node.Val, max)
}

104. 二叉树的最大深度

func maxDepth(root *TreeNode) int {
	if root == nil {
		return 0
	}
	return max(maxDepth(root.Left), maxDepth(root.Right)) + 1
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

111. 二叉树的最小深度

func minDepth(root *TreeNode) int {
	if root == nil{
        return 0
    }
    if root.Left == nil && root.Right == nil {
        return 1
    }
	ans := math.MaxInt64
	if root.Left != nil {
		ans = min(ans, minDepth(root.Left))
	}
	if root.Right != nil {
		ans = min(ans, minDepth(root.Right))
	}

	return ans + 1
}

func min(a, b int) int {
	if a > b {
		return b
	}
	return a
}

236. 二叉树的最近公共祖先

 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 right
    }
    return left
}

todo 重新理解 前中序

105. 从前序与中序遍历序列构造二叉树

func buildTree(preorder []int, inorder []int) *TreeNode {
	if len(preorder) == 0 {
		return nil
	}

	root := &TreeNode{preorder[0], nil, nil}
	i := 0
	for ; i < len(inorder); i++ {
		if inorder[i] == preorder[0] {
			break
		}
	}

	root.Left = buildTree(preorder[1:len(inorder[:i])+1], inorder[:i])
	root.Right = buildTree(preorder[len(inorder[:i])+1:], inorder[i+1:])
	return root
}

77. 组合

func combine(n int, k int) [][]int {
    ans := [][]int{}
    helper(n, k, 1, []int{}, &ans)
    return ans
}

func helper(n, k, s int, path []int, ans *[][]int) {
    if len(path) == k {
        tmp := make([]int, k)
        copy(tmp, path)
        *ans = append(*ans, tmp)
        return
    }

    for i := s; i<=n; i++ {
        path = append(path, i)
        helper(n, k, i+1, path, ans)
        path = path[:len(path)-1]
    }
}