96不同的二叉搜索树

一、前言

分类:动态规划。

问题来源LeetCode 96 难度:中等。

问题链接:https://leetcode-cn.com/problems/unique-binary-search-trees/

 

二、题目

给定一个整数 n,生成所有由 1 ... n 为节点所组成的 二叉搜索树 。

示例:

输入:3
输出:5
解释:
以上的输出对应以下 5 种不同结构的二叉搜索树:
   1         3     3      2      1
    \       /     /      / \      \
     3     2     1      1   3      2
    /     /       \                 \
   2     1         2                 3

 

三、思路

这里提供两种解题方法:递归和非递归(动态规划)。思路和《95不同的二叉搜索树II》类似但是更简单。

 

四、编码实现

//==========================================================================
/*
* @file    : 96_NumTrees.h
* @blogs   : https://blog.csdn.net/nie2314550441/article/details/107215516
* @author  : niebingyu
* @date    : 2020/07/08
* @title   : 96不同的二叉搜索树
* @purpose : 给定一个整数 n,求以 1 ... n 为节点组成的二叉搜索树有多少种?
*
* 示例:
* 输入:3
* 输出:5
* 解释:
* 以上的输出对应以下 5 种不同结构的二叉搜索树:
*   1         3     3      2      1
*    \       /     /      / \      \
*     3     2     1      1   3      2
*    /     /       \                 \
*   2     1         2                 3
*
*
* 来源:力扣(LeetCode)
* 难度:中等
* 链接:https://leetcode-cn.com/problems/unique-binary-search-trees/
*/
//==========================================================================
#pragma once
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

#define NAMESPACE_NUMTREES namespace NAME_NUMTREES {
#define NAMESPACE_NUMTREESEND }
NAMESPACE_NUMTREES

// 递归
class Solution_1 
{
public:
    int numTrees(int n) 
    {
        if (n == 0) 
            return 0;

        return helper(1, n);
    }

    int helper(int start, int end) 
    {
        if (start > end) 
            return 1;
		
        int sum = 0;
        for (int i = start; i <= end; i++) 
        {
            sum += helper(start, i - 1) * helper(i + 1, end);
        }

        return sum;
    }
};

// 动态规划:非递归版本的拷贝树实现
// 递推公式:dp[n] = dp[i-1]*dp[n-i]; i=[1,n]。结果需要进行拷贝
class Solution_2 
{
public:
    int numTrees(int n) 
    {
        if (n == 0) 
            return 0;

        vector<int> dp(n + 1);
        dp[0] = 1, dp[1] = 1;

        for (int i = 2; i <= n; i++) 
        {
            for (int j = 1; j <= i; j++) 
            {
                dp[i] += dp[j - 1] * dp[i - j];
            }
        }

        return dp[n];
    }
};

以下为测试代码//
// 测试 用例 START
void test(const char* testName, int n, int expect)
{
    Solution_1 s1;
    int result1 = s1.numTrees(n);

    Solution_2 s2;
    int result2 = s2.numTrees(n);

    if (expect == result1 && expect == result2) // 简单校验一下
        cout << testName << ", solution passed." << endl;
    else
        cout << testName << ", solution failed. expect: "<< expect << ",result1: "<< result1 <<",result2:" << result2 << endl;
}

// 测试用例
void Test1()
{
    int n = 1;
    int expect = 1;
    
    test("Test1()", n, expect);
}

void Test2()
{
    int n = 2;
    int expect = 2;

    test("Test2()", n, expect);
}

void Test3()
{
    int n = 3;
    int expect = 5;

    test("Test3()", n, expect);
}

void Test4()
{
    int n = 4;
    int expect = 14;

    test("Test4()", n, expect);
}

NAMESPACE_NUMTREESEND
// 测试 用例 END
//

void NumTrees_Test()
{
	NAME_NUMTREES::Test1();
	NAME_NUMTREES::Test2();
	NAME_NUMTREES::Test3();
	NAME_NUMTREES::Test4();
}

执行结果:

 

 

 

 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值