当前位置:网站首页>Generate binary tree according to preorder & inorder traversal [partition / generation / splicing of left subtree | root | right subtree]
Generate binary tree according to preorder & inorder traversal [partition / generation / splicing of left subtree | root | right subtree]
2022-06-24 14:06:00 【REN_ Linsen】
Binary tree generation -> Recursive partition + Backtracking splicing
Preface
The generation of binary tree , Generally speaking, it is analyzed from top to bottom, that is, divided into , Splicing subtrees from bottom to top , Finally, we get the whole binary tree . Examine the division of left and right subtrees + The left and right subtrees trace back and splice knowledge points .
One 、 According to the preface & Middle order traversal generates binary tree
Two 、 Recursive partition + Backtracking splicing subtree
package everyday.medium;
import java.util.HashMap;
import java.util.Map;
// Using preorder and inorder traversal to construct binary tree .
public class BuildTree {
/* target: Using preorder and inorder traversal to construct binary tree . Antecedent function ? Get all the root nodes on the left subtree in turn . Middle order function ? The left and right subtrees are divided by the root node traversed by the previous order , Continuous division , Generate a binary tree from bottom to top . */
public TreeNode buildTree(int[] preorder, int[] inorder) {
// assert No repeating elements .
Map<Integer, Integer> m = new HashMap<>();
// preparation , Get the root node location quickly , Divide the left and right subtrees .
for (int i = 0; i < inorder.length; i++) m.put(inorder[i], i);
// Building tree
return buildTree(preorder, inorder, 0, inorder.length - 1, m);
}
int idx = 0;
private TreeNode buildTree(int[] preorder, int[] inorder, int begin, int end, Map<Integer, Integer> m) {
if (begin > end) return null;
// Use the root node to divide the left and right subtrees , Root node by preorder[idx] Come to .
int mid = m.get(preorder[idx++]);
// Get recursively generated left and right children .
TreeNode left = buildTree(preorder, inorder, begin, mid - 1, m);
TreeNode right = buildTree(preorder, inorder, mid + 1, end, m);
// Generate root node .
TreeNode root = new TreeNode(inorder[mid]);
// Join the left and right subtrees .
root.left = left;
root.right = right;
// Back to the spliced root Trees .
return root;
}
// 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) The generation of binary tree , Generally speaking, it is analyzed from top to bottom, that is, divided into , Splicing subtrees from bottom to top , Finally, we get the whole binary tree .
2) The preorder traversal function recursively determines the root nodes of the left and right subtrees in turn , In the sequence traversal + The root node can divide the left and right subtrees .
reference
[1] LeetCode According to the preface & Middle order traversal generates binary tree
边栏推荐
- Mysql题目篇
- Usage of multimeter
- 90%的项目经理都跳过的坑,你现在还在坑里吗?
- Jericho turns on shouting in all modes to increase mic automatic mute [chapter]
- Google waymo proposed r4d: remote distance estimation using reference target
- Harmony os. (2)
- 2022年煤炭生产经营单位(安全生产管理人员)考试题模拟考试平台操作
- Télétravail: Camping à la maison gadgets de bureau | rédaction communautaire
- Vulnerability management mistakes that CIOs still make
- 杰理之在所有模式下打开喊话增加 mic 自动 mute【篇】
猜你喜欢
AutoRF:从单视角观察中学习3D物体辐射场(CVPR 2022)
万用表的使用方法
群晖向阿里云OSS同步
NPM package [details] (including NPM package development, release, installation, update, search, uninstall, view, version number update rules, package.json details, etc.)
Research and development practice of Kwai real-time data warehouse support system
一键生成大学、专业甚至录取概率,AI填报志愿卡这么神奇?
SAP Marketing Cloud 功能概述(四)
Jerry's serial port receiving IO needs to set the digital function [chapter]
Vulnerability management mistakes that CIOs still make
居家办公更要高效-自动化办公完美提升摸鱼时间 | 社区征文
随机推荐
4个不可不知的“安全左移”的理由
Jerry's serial port receiving IO needs to set the digital function [chapter]
Rongyun communication has "hacked" into the heart of the bank
90%的项目经理都跳过的坑,你现在还在坑里吗?
2022年煤炭生产经营单位(安全生产管理人员)考试题模拟考试平台操作
Ti Xing Shu'an joined the dragon lizard community to jointly create a network security ecosystem
**Puzzling little problem in unity - light and sky box
智慧园区SaaS管理系统解决方案:赋能园区实现信息化、数字化管理
数学之英文写作——基本中英文词汇(几何与三角的常用词汇)
Kotlin composite suspend function
2022年江西省安全员B证考试题库模拟考试平台操作
Android kotlin Encyclopedia
Rasa 3. X learning series - it is a great honor to be a source code contributor of Rasa contributors, and to build and share the rasa community with rasa source code contributors all over the world!
Vulnerability management mistakes that CIOs still make
Hardware development notes (6): basic process of hardware development, making a USB to RS232 module (5): creating USB package library and associating principle graphic devices
位于相同的分布式端口组但不同主机上的虚拟机无法互相通信
2022煤矿瓦斯抽采操作证考试题及模拟考试
kotlin 协程通道
2022年氟化工艺考试模拟100题及答案
redis 数据类型详解