LeetCode96_Unique Binary Search Trees(求1到n这些节点可以组成多少种不同的二叉查找树) Java题解

题目:

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

解题:

用递归的思想,当只有0个或是1个节点的时候,只有一种;n个节点的时候有f(n)种:

左边可以有n-1个节点,右边0个节点,根据对称性可以左右互换,这时候有2*f(n-1)*f(0);

一边1个,另一边n-2个,这时候有2*f(1)*f(n-2);

一边两个,一边N-3个,这时候有2*f(2)*f(n-3);

。。。。。。

如果n为奇数,两边都是n/2个,这时候有f(n/2)*f(n/2),如果n为偶数,一边n/2一边(n/2+1)个,为2*f(n/2)*f(n/2+1);

代码:

  public static int numTrees(int n) {
    	 if(n==1||n==0)
    		 return 1;
    	 int sum=0;
    	 if(n%2==0)
    	 {
    		 for(int k=n-1;k>=n/2;k--)
        	 {
        		 sum+=2*numTrees(k)*numTrees(n-1-k);
        	 }
        	 return sum;

    	 }
    	 else {
    		 for(int k=n-1;k>n/2;k--)
        	 {
        		 sum+=2*numTrees(k)*numTrees(n-1-k);
        	 }
        	 return sum+numTrees(n/2)*numTrees(n/2);
		}

    }

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-07-31 21:11:00

LeetCode96_Unique Binary Search Trees(求1到n这些节点可以组成多少种不同的二叉查找树) Java题解的相关文章

LeetCode96_Unique Binary Search Trees(求1到n这些节点能够组成多少种不同的二叉查找树) Java题解

题目: 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 解题: 用递归的思想,当仅仅有0个或是1个节点的时候.仅仅有一种.n个节点的时候有f(n)种

Unique Binary Search Trees II

Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example,Given n = 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 confused what "{1,#,2,3}&

LeetCode Unique Binary Search Trees

  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,2,3..n对应的值,想到递推,DP,该题也就是如何构造一个二叉查找树,

[C++]LeetCode: 53 Unique Binary Search Trees

题目: 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 背景知识: 二叉查找树(英语:Binary Search Tree),也称二叉搜索树.有序二

【leetcode】Unique Binary Search Trees

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. 注意:二分查找树的定义是,左子树节点均小于root,右子树节点均大于root! 不要想当然地将某个点作为root时,认为其他所有节点都能全部放在left/right中,除非这个点是 min 或者

[leetcode]Unique Binary Search Trees II @ Python

原题地址:https://oj.leetcode.com/problems/unique-binary-search-trees-ii/ 题意:接上一题,这题要求返回的是所有符合条件的二叉查找树,而上一题要求的是符合条件的二叉查找树的棵数,我们上一题提过,求个数一般思路是动态规划,而枚举的话,我们就考虑dfs了.dfs(start, end)函数返回以start,start+1,...,end为根的二叉查找树. 代码: # Definition for a binary tree node #

leetcode 96 Unique Binary Search Trees ----- java

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 给一个正整数n,然后将这n个数进行二叉排序树的排列,求有多少种组合. public class Sol

Leetcode#96Unique Binary Search Trees

Unique Binary Search Trees Total Accepted: 47684 Total Submissions: 131223My Submissions Question Solution 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 uniq

Unique Binary Search Trees,Unique Binary Search Trees2 生成二叉排序树

Unique Binary Search Trees:求生成二叉排序树的个数. 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 算法分析:类似上阶梯,