[leetcode专题]--Tree(1)

本文主要总结leetcode中与Tree相关的题目,并给出了Go语言解法。

94. Binary Tree Inorder Traversal

题目描述:

Given a binary tree, return the inorder traversal of its nodes' values.

Example:

Input: [1,null,2,3]
   1
    \
     2
    /
   3

Output: [1,3,2]

Follow up: Recursive solution is trivial, could you do it iteratively?

解法如下,主要利用了stack。

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func inorderTraversal(root *TreeNode) (ret []int) {
    if root == nil{
        return
    }
    
    var stack []*TreeNode
    cur := root
    
    for cur!=nil || len(stack) > 0{
        if cur != nil{
            stack = append(stack,cur)
            cur = cur.Left
        }else{
            cur  = stack[len(stack)-1]
            stack = stack[:len(stack)-1]
            ret = append(ret,cur.Val)
            cur = cur.Right
        }
    }
    return
}

96. Unique Binary Search Trees

题目描述:

Given n, how many structurally unique BST (binary search trees) that store values 1 ... n?

Example:

Input: 3
Output: 5
Explanation:
Given n = 3, there are a total of 5 unique BST's:

   1         3     3      2      1
    \       /     /      / \      \
     3     2     1      1   3      2
    /     /       \                 \
   2     1         2                 3

本题要求给出能够存储整数1到n的BST的个数。解题思路是运用DP动态规划。

func numTrees(n int) int {
    if n == 0{
        return 0
    }
    
    var dp []int
    //dp[0] = 1
    dp = append(dp,1)
    
    for i:=1;i <= n;i++{
        dp = append(dp,0)
        for j := 0; j < i; j++{
            dp[i] += dp[j]*dp[i-1-j]
        }
    }
    
    return dp[n]
}

95. Unique Binary Search Trees II

题目描述:

Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ... n.

Example:

Input: 3
Output:
[
  [1,null,3,2],
  [3,2,null,1],
  [3,1,null,null,2],
  [2,1,3],
  [1,null,2,null,3]
]
Explanation:
The above output corresponds to the 5 unique BST's shown below:

   1         3     3      2      1
    \       /     /      / \      \
     3     2     1      1   3      2
    /     /       \                 \
   2     1         2                 3

本题要求列出所有可能的BST。对于这类问题,主要的思路是DFS。

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func generateTrees(n int) []*TreeNode {
    var ret []*TreeNode
    if n < 1{
        return ret
    }
    return genBST(1,n)
}

func genBST(min, max int) []*TreeNode{
    var ret []*TreeNode
    if min > max{
        ret = append(ret,nil)
        return ret
    }
    
    for i:=min; i <=max; i++{
        leftsubtree := genBST(min,i-1)
        rightsubtree := genBST(i+1,max)
        
        for j:=0;j < len(leftsubtree); j++{
            for k:= 0; k < len(rightsubtree); k++{
                root :=  &TreeNode{Val:i}
                root.Left = leftsubtree[j]
                root.Right = rightsubtree[k]
                ret = append(ret,root)
            }
        }
    }
    
    return ret
}

你可能感兴趣的:([leetcode专题]--Tree(1))