Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve different binary search Tree problems with LeetCode

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report