当前位置:网站首页>Recursive method to construct binary tree from preorder and inorder traversal sequence
Recursive method to construct binary tree from preorder and inorder traversal sequence
2022-07-07 08:04:00 【Hydrion-Qlz】
105. Construction of binary trees from traversal sequences of preorder and middle order
Medium difficulty
Given two arrays of integers preorder and inorder , among preorder It's a binary tree The first sequence traversal , inorder It's from the same tree In the sequence traversal , Please construct a binary tree and return its root node .
Ideas
The knowledge points that need to be understood are :
- In the middle order traversal, the left side of a node is all nodes of its left subtree , On the right are all the nodes of its right subtree
- The first node in the preorder traversal is the current node , Its left subtree and right subtree are on its right
We can find the current node according to the previous traversal , Then find the position of the current root node in the ordered array , Then the range of the new left subtree and right subtree is determined by the position of the root node and the range of its subtree , Then recurs continuously , Until the last traversal of the entire array .
We cannot determine the range of left subtree and right subtree in the preceding traversal , But you can get this value from the middle order traversal
Specific please see 32 And 33 That's ok
package cn.edu.xjtu.carlWay.tree.preAndInConstructBinaryTree;
import cn.edu.xjtu.Util.TreeNode.TreeNode;
/** * 105. Construction of binary trees from traversal sequences of preorder and middle order * Given two arrays of integers preorder and inorder , among preorder Is the prior traversal of a binary tree , inorder Is the middle order traversal of the same tree , Please construct a binary tree and return its root node . * <p> * https://leetcode-cn.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ */
public class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
return helpBuild(preorder, 0, preorder.length, inorder, 0, inorder.length);
}
private TreeNode helpBuild(int[] preorder, int preLeft, int preRight, int[] inorder, int inLeft, int inRight) {
if (inRight - inLeft == 0) {
return null;
}
if (inRight - inLeft == 1) {
return new TreeNode(inorder[inLeft]);
}
TreeNode root = new TreeNode(preorder[preLeft]);
int rootIndex = 0;
for (int i = inLeft; i < inRight; i++) {
if (inorder[i] == root.val) {
rootIndex = i;
break;
}
}
root.left = helpBuild(preorder, preLeft + 1, preLeft + 1 + (rootIndex - inLeft), inorder, inLeft, rootIndex);
root.right = helpBuild(preorder, preLeft + 1 + (rootIndex - inLeft), preRight, inorder, rootIndex + 1, inRight);
return root;
}
}
边栏推荐
- The element with setfieldsvalue set is obtained as undefined with GetFieldValue
- [UVM practice] Chapter 2: a simple UVM verification platform (2) only driver verification platform
- 【VHDL 并行语句执行】
- 2022茶艺师(初级)考试题模拟考试题库及在线模拟考试
- Force buckle 144 Preorder traversal of binary tree
- CTF daily question day43 rsa5
- LeetCode 40:组合总和 II
- Linux server development, SQL statements, indexes, views, stored procedures, triggers
- Pytest+allure+jenkins environment -- completion of pit filling
- Regular e-commerce problems part1
猜你喜欢

2022制冷与空调设备运行操作复训题库及答案

PHP exports millions of data

探索干货篇!Apifox 建设思路

Qt学习27 应用程序中的主窗口
![[CV] Wu Enda machine learning course notes | Chapter 8](/img/c0/7a39355fb3a6cb506f0fbcf2a7aa24.jpg)
[CV] Wu Enda machine learning course notes | Chapter 8

Detailed explanation of Kalman filter for motion state estimation

Qt学习28 主窗口中的工具栏

2022年全国最新消防设施操作员(初级消防设施操作员)模拟题及答案

探索Cassandra的去中心化分布式架构

Cnopendata list data of Chinese colleges and Universities
随机推荐
Cnopendata American Golden Globe Award winning data
game攻防世界逆向
2022年全国最新消防设施操作员(初级消防设施操作员)模拟题及答案
微信小程序基本组件使用介绍
pytest+allure+jenkins環境--填坑完畢
Implementation of replacement function of shell script
C language flight booking system
QT learning 26 integrated example of layout management
[UVM foundation] what is transaction
2022 National latest fire-fighting facility operator (primary fire-fighting facility operator) simulation questions and answers
Thinkcmf6.0 installation tutorial
Explore Cassandra's decentralized distributed architecture
Li Kou interview question 04.01 Path between nodes
有 Docker 谁还在自己本地安装 Mysql ?
Leetcode 90: subset II
Button wizard collection learning - mineral medicine collection and running map
2022年茶艺师(中级)考试试题及模拟考试
Pytorch parameter initialization
The element with setfieldsvalue set is obtained as undefined with GetFieldValue
Linux server development, MySQL stored procedures, functions and triggers