Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to solve different binary search Tree problems with LeetCode

Shulou Source: shulou.com Published: 2022-06-01 13:36:36 09月26日 Update

This article mainly shows you "LeetCode how to solve different binary search tree problems", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "LeetCode how to solve different binary search tree problems" this article.

Topic description

Given an integer n, find 1. How many binary search trees are there with n nodes?

Example:

Input: 3 output: 5 explanation: given n = 3, a binary search tree with 5 different structures:

1, 3, 3, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 3, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 3, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2,

Tags: dynamic plannin

Suppose that the number of binary sort trees with n nodes is G (n), and let f (I) be the number of binary search trees with I as root, then

When I is the root node, the number of left subtree nodes is iMu1, and the right subtree node is nMui, then

The formula of Cateran number [1] can be obtained by combining two formulas.

The algorithm dynamic graph code class Solution {public int numTrees (int n) {int [] dp = new int [n = 1]; dp [0] = 1; dp [1] = 1; for (int I = 2; I < n = 1; for +) for (int j = 1; j < I + 1; j +) dp [I] + = DP [j-1] * dp [iqu j] Return dp [n];}} these are all the contents of the article "how LeetCode solves different binary search tree problems". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Search node different problem number content article formula subtree learning help two code dynamic Cartland ideas integers solutions easy to understand more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi NVidia Shulou Information Microsoft Linux