不同的二叉搜索树&II

不同的二叉搜索树

只要求个数,递推根节点分割左右子树即可

class Solution {
    public int numTrees(int n) {
       int []dp=new int[n+1];
       for(int i=1;i<=n;i++){
           if(i==1||i==2)
               dp[i]=i;
           else{
               for(int j=1;j<=i;j++)
                   if(j>1&&j<i)//有左子树和右子树
                       dp[i]+=dp[j-1]*dp[i-j];
                   else if(j==1)//只有右子树
                       dp[i]+=dp[i-j];
                   else
                       dp[i]+=dp[j-1];
           }
       }
       return dp[n];
    }
}

不同的二叉搜索树 II

要求求具体的树,还是同上思想

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
public List<TreeNode> generateTrees(int n) {
    if(n == 0)
        return new LinkedList<TreeNode>();
    return generateTrees(1,n);
}
public List<TreeNode> generateTrees(int start,int end) {
    List<TreeNode> res = new LinkedList<TreeNode>();
    if(start > end){
        res.add(null);
        return res;
    }
    for(int i = start;i <= end;i++){
        List<TreeNode> subLeftTree = generateTrees(start,i-1);
        List<TreeNode> subRightTree = generateTrees(i+1,end);
        for(TreeNode left : subLeftTree){
            for(TreeNode right : subRightTree){
                TreeNode node = new TreeNode(i);
                node.left = left;
                node.right = right;
                res.add(node);
            }
        }
    }
    return res;
}
}

原文地址:https://www.cnblogs.com/yuelien/p/10592940.html

时间: 2024-08-30 14:12:34

不同的二叉搜索树&II的相关文章

95. 不同的二叉搜索树 II

95. 不同的二叉搜索树 II 题目描述 给定一个整数 n,生成所有由 1 ... n 为节点所组成的二叉搜索树. 示例: 输入: 3 输出: [ [1,null,3,2], [3,2,null,1], [3,1,null,null,2], [2,1,3], [1,null,2,null,3] ] 解释: 以上的输出对应以下 5 种不同结构的二叉搜索树: 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 贴出代码 /** * Definition for

力扣第95题 不同的二叉搜索树II

力扣第95题 不同的二叉搜索树II 给定一个整数 n,生成所有由 1 ... n 为节点所组成的二叉搜索树. struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; vector<TreeNode*> generateTree(int start, int end) { vector<TreeNode*> v

LeetCode95. 不同的二叉搜索树 II

给定一个整数 n,生成所有由 1 ... n 为节点所组成的二叉搜索树. 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11

LeetCode(95): 不同的二叉搜索树 II

Medium! 题目描述: 给定一个整数 n,生成所有由 1 ... n 为节点所组成的二叉搜索树. 示例: 输入: 3 输出: [   [1,null,3,2],   [3,2,null,1],   [3,1,null,null,2],   [2,1,3],   [1,null,2,null,3] ] 解释: 以上的输出对应以下 5 种不同结构的二叉搜索树: 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 解题思路: 这种建树问题一般来说都是用递归来

Leetcode 95.不同的二叉搜索树II

给定一个整数 n,生成所有由 1 ... n 为节点所组成的二叉搜索树. 示例: 输入: 3 输出: [   [1,null,3,2],   [3,2,null,1],   [3,1,null,null,2],   [2,1,3],   [1,null,2,null,3] ] 解释: 以上的输出对应以下 5 种不同结构的二叉搜索树: 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 1 /** 2 * Definition for a binary tr

二叉树-二叉搜索树(中序)

题型: (1)验证 (2)修复(排列不正确,修复) (3)构造(给排列求树-平衡的:种类) (4)利用性质求第n个结点 二叉搜索树的思路:中序输出+相关操作 如果要求空间复杂度O(1),考虑莫里斯遍历 98. 验证二叉搜索树    面试题 04.05. 合法二叉搜索树 (1) 思路:中序排列,看次序 1 class Solution { 2 public boolean isValidBST(TreeNode root) { 3 // if(root==null){ // 题目空树是true 4

leetCode 95.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 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}&

C++实现二叉搜索树的常用操作

实现操作 (1)二叉搜索树的建立 (2)二叉搜索树的插入 (3)二叉搜索树的三种递归遍历(前序.中序和后续) (4)二叉搜索树的三种非递归遍历(前序.中序和后续) (5)二叉搜索树的逐层打印 (6)搜索某一个字符(递归算法) (7)搜索一个字符(非递归算法) (8)查找最大元素 (9)查找最小元素 有时间再实现: (10)二叉搜索树的前驱和后继查找 (11)二叉搜索树的删除 源码分析: #include <iostream> #include <stack> #include &l