资讯专栏INFORMATION COLUMN

leetcode95-96 Unique Binary Search Trees I-II

morgan / 1619人阅读

摘要:在这里我们使用数组中下标为的位置来记录个元素可以组成的平衡二叉树的数量。在递归的过程中,我们找到以当前节点作为根节点的所有平衡二叉树,并将结果以形式返回上一级调用。

题目要求
Given n, how many structurally unique BST"s (binary search trees) that store values 1...n?

For example,
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这n-1个数字可以构成的全部搜索二叉树以及其个数。

思路和代码

如果只是单纯的计算二叉树的数量,其实这就完全转化成了一道规律题。我们可以从1开始寻找规律。
1: 1
1,2: 12, 21
1,2,3:123,132,213,312,321

我们可以通过dp的方式来记录。无论以哪一个节点作为root节点,它的左子树的元素和右子树的元素都是固定的。也就是说,假设root值为i,那么左子树的元素为[1...i-1],右子树的元素为[i+1...n]。因此当前root节点可以生成的平衡二叉树数量即为左子树数量*右子树数量。在这里我们使用int[]数组中下标为n的位置来记录n个元素可以组成的平衡二叉树的数量

    public int numTrees(int n) {
        int[] nums = new int[n+1];
        for(int i = 0 ; i <= n ; i++){
            if(i==0 || i==1) nums[i] = 1;
            else{
                for(int j = 1 ; j<=i ; j++){
                    nums[i] += nums[j-1]*nums[i-j];
                }
            }
        }
        return nums[n];
    }

如果要我们返回具体树的形态的话,就需要我们通过backtracking的递归形式来找到所有的平衡二叉树。在递归的过程中,我们找到以当前节点作为根节点的所有平衡二叉树,并将结果以list形式返回上一级调用。

    public List generateTrees(int n) {
        if(n==0) return new ArrayList();
        return generateTrees(1,n);
    }
    public List generateTrees(int start, int end){
        List result = new ArrayList();
        if(start>end){
            result.add(null);
        }else if(start==end){
            result.add(new TreeNode(start));
        }else{
            for(int i = start ; i<=end ; i++){
                List left = generateTrees(start, i-1);
                List right = generateTrees(i+1, end);
                for(TreeNode tempLeft : left){
                    for(TreeNode tempRight : right){
                        TreeNode root = new TreeNode(i);
                        root.left = tempLeft;
                        root.right = tempRight;
                        result.add(root);
                    }
                }
            }
            
        }
        return result;
        
    }


想要了解更多开发技术,面试教程以及互联网公司内推,欢迎关注我的微信公众号!将会不定期的发放福利哦~

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/67545.html

相关文章

  • [LeetCode] 96. Unique Binary Search Trees I &

    Unique Binary Search Trees Problem Given n, how many structurally unique BSTs (binary search trees) that store values 1...n? Example Given n = 3, there are a total of 5 unique BSTs. 1 3 3...

    nidaye 评论0 收藏0
  • [Leetcode] Unique Binary Search Trees 唯一二叉搜索树

    摘要:而根可以选择从到的任意的数,唯一二叉树的总数,就是根为到的树相加。所以该问题化简为以为根,其唯一左子树和右子树各有多少,这就是个动态规划的问题了。 Unique Binary Search Trees I && II 解法请见:https://yanjia.li/zh/2019/02/... Given n, how many structurally unique BSTs (b...

    enrecul101 评论0 收藏0
  • leetcode-95-Unique Binary Search Trees II

    摘要:题目解读穷举列出所有二叉树的结构类型。重点动态规划,关注临近,,之间的关系应用穷举组合,动态规划穷举组合,适用于相邻元素有规律。处注意边界值的情况。不能有重复,遗漏。 题目解读: 穷举列出所有二叉树的结构类型。重点: 动态规划,关注临近root,left,right之间的关系应用:穷举组合,动态规划穷举组合,适用于相邻元素有规律。bug处:注意边界值的情况。不能有重复,遗漏。 clas...

    Tony_Zby 评论0 收藏0
  • [Leetcode-Dynamic Programming]Unique Binary Search

    Unique Binary Search TreesGiven n, how many structurally unique BSTs (binary search trees) that store values 1...n? For example,Given n = 3, there are a total of 5 unique BSTs. 1 3 3 ...

    MartinDai 评论0 收藏0
  • 前端 | 每天一个 LeetCode

    摘要:在线网站地址我的微信公众号完整题目列表从年月日起,每天更新一题,顺序从易到难,目前已更新个题。这是项目地址欢迎一起交流学习。 这篇文章记录我练习的 LeetCode 题目,语言 JavaScript。 在线网站:https://cattle.w3fun.com GitHub 地址:https://github.com/swpuLeo/ca...我的微信公众号: showImg(htt...

    张汉庆 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<