当前位置:网站首页>Leetcode 105. construct binary tree from preorder and inorder traversal sequence & 106. construct binary tree from inorder and postorder traversal sequence
Leetcode 105. construct binary tree from preorder and inorder traversal sequence & 106. construct binary tree from inorder and postorder traversal sequence
2022-07-28 14:07:00 【Grilled little fat sheep with charcoal...】
Construction of binary trees from traversal sequences of preorder and middle order

Code implementation :
import java.util.HashMap;
import java.util.Map;
/** * @Author Mr.Lu * @Date 2022/7/27 11:36 * @ClassName InorderAndPostOrderBuildTreeTest * @Version 1.0 */
public class InorderAndPreOrderBuildTreeTest {
Map<Integer, Integer> map; // It is convenient to find the position according to the value
public TreeNode buildTree(int[] preorder, int[] inorder){
map = new HashMap<>();
for (int i = 0; i < inorder.length; i++) {
// use map Save the corresponding position of the values of the middle order sequence
map.put(inorder[i], i);
}
return findNode(inorder, 0, inorder.length, preorder, 0, preorder.length); // Before closed after opening
}
private TreeNode findNode(int[] inorder, int inBegin, int inEnd, int[] preorder, int preBegin, int preEnd) {
// The ranges in the parameters are all front closed and back open
if(preBegin >= preEnd || inBegin >= inEnd){
// Not satisfied with left close right open , There is no element , Returns an empty tree
return null;
}
int rootIndex = map.get(preorder[preBegin]); // Find the position of the first element in the preorder traversal
TreeNode root = new TreeNode(inorder[rootIndex]); // Tectonic node
int lenOfLeft = rootIndex - inBegin; // Save the number of left subtrees in the middle order , Used to determine the number of preordered sequences
root.left = findNode(inorder, inBegin, rootIndex, preorder, preBegin + 1, preBegin + lenOfLeft + 1); // The left array of sequence traversal and preorder traversal in recursive processing
root.right = findNode(inorder, rootIndex + 1, inEnd, preorder, preBegin + lenOfLeft + 1, preEnd); // The right array of sequence traversal and preorder traversal in recursive processing
return root;
}
}
Construct binary tree from middle order and post order traversal sequence

Code implementation :
import java.util.HashMap;
import java.util.Map;
/** * @Author Mr.Lu * @Date 2022/7/27 11:36 * @ClassName InorderAndPostOrderBuildTreeTest * @Version 1.0 */
public class InorderAndPostOrderBuildTreeTest {
Map<Integer, Integer> map; // It is convenient to find the position according to the value
public TreeNode buildTree(int[] inorder, int[] postorder){
map = new HashMap<>();
for(int i = 0; i < inorder.length; i++){
map.put(inorder[i], i);
}
return findNode(inorder, 0, inorder.length, postorder, 0, postorder.length);
}
private TreeNode findNode(int[] inorder, int inBegin, int inEnd, int[] postorder, int postBegin, int postEnd) {
// The ranges in the parameters are all front closed and back open
if(inBegin >= inEnd || postBegin >= postEnd){
// Not satisfied with left close right open , There is no element , Go straight back to
return null;
}
int rootIndex = map.get(postorder[postEnd - 1]); // Find the position of the last element of the subsequent traversal in the middle order traversal
TreeNode root = new TreeNode(inorder[rootIndex]); // Tectonic node
int lenOfLeft = rootIndex - inBegin; // Save the number of left subtrees in the middle order , Used to determine the number of subsequent series
root.left = findNode(inorder, inBegin, rootIndex, postorder, postBegin, postBegin + lenOfLeft);
root.right = findNode(inorder,rootIndex + 1, inEnd, postorder, postBegin + lenOfLeft, postEnd - 1);
return root;
}
}
Reference resources : Random code recording https://programmercarl.com/0106.%E4%BB%8E%E4%B8%AD%E5%BA%8F%E4%B8%8E%E5%90%8E%E5%BA%8F%E9%81%8D%E5%8E%86%E5%BA%8F%E5%88%97%E6%9E%84%E9%80%A0%E4%BA%8C%E5%8F%89%E6%A0%91.html#java
边栏推荐
猜你喜欢

一文读懂如何部署具有外部数据库的高可用 K3s

算法---不同路径(Kotlin)

30 day question brushing plan (IV)

Qt5开发从入门到精通——第一篇概述

Understanding of "image denoising using an improved generic advantageous network with Wasserstein distance"

Clickhouse架构与设计

Jmeter安装教程及登录增加token

SLAM论文合集

Security assurance is based on software life cycle -psp application

30 day question brushing plan (III)
随机推荐
多线程与高并发(三)—— 源码解析 AQS 原理
Several solutions to spanning
Duplicate data in leetcode (442) array
Long closed period private placement products reappearance industry insiders have different views
30 day question brushing training (I)
【飞控开发基础教程7】疯壳·开源编队无人机-SPI(气压计数据获取)
第六章 支持向量机
Qt5 development from introduction to mastery -- the first overview
Verification code brute force cracking test [easy to understand]
R语言使用dpois函数生成泊松分布密度数据、使用plot函数可视化泊松分布密度数据(Poisson distribution)
基于NoneBot2的qq机器人配置记录
Security assurance is based on software life cycle -psp application
Master several common sorting - Select Sorting
解决跨越的几种方案
SLAM论文合集
Rolling update strategy of deployment.
Master closures and consolidate basic skills
协同办公工具:在线白板初起步,在线设计已红海
Security assurance is based on software life cycle -istio authentication mechanism
Tutorial on the principle and application of database system (059) -- MySQL exercise questions: operation questions 1-10 (III)