当前位置:网站首页>Generate all possible binary search trees
Generate all possible binary search trees
2022-06-23 23:51:00 【REN_ Linsen】
Generate all possible binary search trees
Preface
For tree problems , Using its recursion property is the basis of solving problems , Generate subtree from bottom to top , The left and right subtrees are combined into any subtree in the form of Cartesian product , In this way, the left and right subtrees and root nodes are handled , Then the whole tree will be handled .
One 、 Different binary search trees II

Two 、 Recursively generate backtracking stitches
package everyday.medium;
import java.util.ArrayList;
import java.util.List;
// Different binary search trees II
public class GenerateTrees {
/* target: Given n Nodes , Generate any binary search tree . It is divided into left and right sections , Represent left and right subtrees respectively , Generate subtree from bottom to top, return and assemble in the form of Cartesian product , Up to the primary root node . */
public List<TreeNode> generateTrees(int n) {
return generateTrees(1, n);
}
private List<TreeNode> generateTrees(int begin, int end) {
List<TreeNode> ans = new ArrayList<>();
// Recursive export , When begin > end when , Returns the set of left and right subtrees , But only null node .
if (begin > end) {
ans.add(null);
return ans;
}
// Divide the set and assemble .
// Take each position as the head node .
for (int i = begin; i <= end; i++) {
// Get all the generation conditions of the left and right subtrees .
List<TreeNode> leftNodes = generateTrees(begin, i - 1);
List<TreeNode> rightNodes = generateTrees(i + 1, end);
// Join the Cartesian product of left and right subtrees .
for (TreeNode left : leftNodes) {
for (TreeNode right : rightNodes) {
TreeNode root = new TreeNode(i);
root.left = left;
root.right = right;
ans.add(root);
}
}
}
// Return all possible spanning trees .
return ans;
}
// Definition for a binary tree node.
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {
}
TreeNode(int val) {
this.val = val;
}
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
}
summary
1) Making good use of the recursive characteristics of binary trees is the basic skill .
2) Handle any subtree , And you're done with the whole tree .
3) The generation of trees , Half of them are made by backtracking and splicing root nodes from bottom to top .
reference
边栏推荐
- APP性能优化之启动流程分析
- 高仿书旗小说 Flutter 版,学起来
- Flux in three dimensional vector field
- Idea automatically generates unit tests, doubling efficiency!
- Bitmap load memory analysis
- 7、STM32——LCD
- Web site SSL certificate
- Acrel-3000WEB电能管理系统在都巴高速的应用
- 微信录制视频转圈效果如何实现?
- Application of acrel-3000web power management system in Duba Expressway
猜你喜欢

2022山东健博会,济南国际大健康产业博览会,中国营养健康展

What are the good solutions for industrial control safety of production line

List<? Extensions T > and list <? Super T > difference

Stm32----- timer

Multi store drug inventory system source code large chain drugstore management system source code

Digital property management has become a trend. How can traditional property companies achieve digital butterfly change through transformation?

Bitmap load memory analysis

高仿书旗小说 Flutter 版,学起来

高仿書旗小說 Flutter 版,學起來

Web site SSL certificate
随机推荐
Notepad++实用功能分享(正则行尾行首替换常用方法、文本比对功能等)
7、STM32——LCD
2022 information security engineer examination knowledge point: access control
APP性能优化之启动流程分析
The lower left corner of vs QT VTK displays the synchronized minor coordinate axis
IDEA 自动生成单元测试,效率倍增!
Setting method of bar code local segment data variable
图扑软件智慧风电:数字孪生 3D 风机智能设备运维
【HackTheBox】Fawn
ACM. Hj89 24 point operation ●●●
完整开源项目之诗词吧 APP
复原IP地址[标准回溯+标准剪枝]
不同物体使用同一材质,有不同的表现
Preliminary understanding of 3D printing and laser cutting process
高仿斗鱼 APP
牛客网:接雨水的双指针问题
What is the same origin policy?
Stm32-------adc (voltage detection)
Why can't the netherworld fortress machine be remotely connected to the server? What are the ways to solve such problems?
The group procurement management system of daily chemical supplies industry changes the traditional procurement mode and reduces the procurement cost