当前位置:网站首页>Middle order and post order traversal to construct binary tree [recursive partition interval and backtracking splicing subtree + similarity and difference between middle post order and middle pre orde
Middle order and post order traversal to construct binary tree [recursive partition interval and backtracking splicing subtree + similarity and difference between middle post order and middle pre orde
2022-06-29 15:28:00 【REN_ Linsen】
A binary tree is generated in the middle and later order
Preface
According to a sequence , You can recursively divide the intervals , Backtracking and splicing subtrees to complete tree generation .
Pre middle order traversal , A node that can determine the root node , One can hold the root node to divide the left and right intervals , Completed the pre task abstractly , So the first middle order can determine a binary search tree . Empathy , The same is true of the middle and later sequence , Do not traverse the root node after the sequence , And the traversal is left and right root , So when the root passes, it is right , So we need to build the right subtree first , Then build the left subtree .
One 、 Middle order and post order traversal to build a binary search tree

Two 、 From top to bottom + Bottom up
// According to the middle order + After the order to build a binary tree .
class BuildTree2 {
/* target: Use post order and middle order traversal to construct binary tree . Aftereffect ? All root nodes on the left subtree are obtained from right to left . Middle order function ? The right and left subtrees are divided by the root nodes traversed in the following order , Continuous division , Generate a binary tree from bottom to top . */
public TreeNode buildTree(int[] inorder, int[] postorder) {
// 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
idx = postorder.length - 1;
return buildTree(inorder, postorder, 0, inorder.length - 1, m);
}
int idx = 0;
private TreeNode buildTree(int[] inorder, int[] postorder, 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(postorder[idx--]);
// Get recursively generated left and right children .
// Be careful , The following traversal is left and right root , So first we get the right subtree , Get the left subtree , This is the difference from the previous sequence traversal location .
TreeNode right = buildTree(inorder, postorder, mid + 1, end, m);
TreeNode left = buildTree(inorder, postorder, begin, mid - 1, 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) Spanning tree foundation , Recursively divide the interval from top to bottom , Join the left and right subtrees from the bottom up .
2) The similarities and differences between the pre middle order traversal spanning tree and the post middle order spanning tree .
reference
[1] LeetCode Middle order and subsequent traversal to build a binary tree
[2] Middle order and pre order traversal to build a binary tree
边栏推荐
猜你喜欢

File常用工具類, 流相關運用 (記錄)

I am 35 years old. Can I change my career to be a programmer?

Ink drop typesetting

render后续来了,封装一个表单往表格中添加数据

MCS: multivariate random variable - discrete random variable

ROS notes (10) - Launch file startup

Lumiprobe 活性染料丨杂染料:BDP FL 神经酰胺

SOFARegistry 源码|数据同步模块解析

打造一个 API 快速开发平台,牛逼!

Unity C basic review 28 - delegation with return (p449)
随机推荐
Is it reliable to invest in REITs funds? Is REITs funds safe
Abnormal logic reasoning problem of Huawei software test written test [2] Huawei hot interview problem
服务器的数据库连不上了【服务已起、防火墙已关、端口已开、netlent 端口不通】
极化SAR几种成像模式
Lumiprobe 脱氧核糖核酸丨磷酸盐 CPG 1000 固体载体
上次给我们发福利的 TDesign ,今天讲讲它的开源故事
bash汇总线上日志
雷达天线简介
Informatics Olympiad all in one 1000: introductory test questions
关于遥感图像解译的思考
卷积神经网络中各层的作用
File常用工具類, 流相關運用 (記錄)
Lumiprobe 点击化学丨非荧光叠氮化物:叠氮化物-PEG3-OH
获取Text组件内容的宽度
Unity C basic review 29 - Generic delegation (p451)
Ink drop typesetting
MCS:多元随机变量——离散随机变量
动态监听DOM元素高度变化
MCS: discrete random variable Pascal Distribution
Lumiprobe 活性染料丨氨基染料:花青5胺