zoukankan      html  css  js  c++  java
  • 95. Unique Binary Search Trees II

    package LeetCode_95
    
    /**
     * 95. Unique Binary Search Trees II
     * https://leetcode.com/problems/unique-binary-search-trees-ii/description/
     * https://zxi.mytechroad.com/blog/uncategorized/leetcode-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]
    ]
     * */
    class TreeNode(var `val`: Int) {
        var left: TreeNode? = null
        var right: TreeNode? = null
    }
    
    class Solution {
        /*
        * solution: Recursion, Time complexity:O(3^n), Space complexity:O(3^n)
        * left sub tree can be generated in the same way for n_l = l..i-1
        * left sub tree can be generated in the same way for n_r = i+1..n
        * */
    
        fun generateTrees(n: Int): List<TreeNode?> {
            if (n == 0) {
                return ArrayList<TreeNode>()
            }
            return genTree(1, n)
        }
    
        private fun genTree(left: Int, right: Int): List<TreeNode?> {
            val result = ArrayList<TreeNode?>()
            if (left > right) {
                result.add(null)
                return result
            }
            for (i in left until right) {
                for (leftTree in genTree(left, i - 1)) {
                    for (rightTree in genTree(i + 1, right)) {
                        val root = TreeNode(i)
                        root.left = leftTree
                        root.right = rightTree
                        result.add(root)
                    }
                }
            }
            return result
        }
    }
  • 相关阅读:
    uva 1374 快速幂计算
    uva 1343 非原创
    uva 11212
    uva 10603
    路径寻找问题……!
    bzoj 1008: [HNOI2008]越狱
    bzoj 1010: [HNOI2008]玩具装箱toy
    dp斜率优化小计
    bzoj 1002[FJOI2007]轮状病毒
    hihocoder #1114
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/13227564.html
Copyright © 2011-2022 走看看