zoukankan      html  css  js  c++  java
  • Leetcode 96. Unique Binary Search Trees

    Unique Binary Search Trees

    class Solution {
    public:
        int numTrees(int n) {
            // We consider the position of node n in the tree first.
            // Because all nodes on the tree except for node n is less than node,
            // if node n has children, that must be its left child.
            // Node n can have a parent if the value of its parent is less than its.
            // So we separate the n - 1 nodes into 2 parts, the first part is considered to be the parent part of node n,
            // and the second part is considered to be the left child part of node n.
            // We traverse every possible separation, for each separation we add the multiplication of the number of the combination
            // of the two parts to the answer of node n.
            vector<int> dp(n + 1, 0);
            dp[0] = 1;
            for (int i = 1; i <= n; i++) {
                for (int j = 0; j < i; j++) {
                    dp[i] += dp[j] * dp[i - 1 - j];
                }
            }
            return dp[n];
        }
    };
    
  • 相关阅读:
    Netty Nio启动全流程
    线性表
    java stream 原理
    SpringBoot 分布式session
    mockito
    RxJava
    HandlerMapping 详解
    SpringMVC 架构
    Spring Cloud 配置服务
    SpringBoot常用配置简介
  • 原文地址:https://www.cnblogs.com/ToRapture/p/12655627.html
Copyright © 2011-2022 走看看