当前位置:网站首页>Recursive method constructs binary tree from middle order and post order traversal sequence
Recursive method constructs binary tree from middle order and post order traversal sequence
2022-07-07 08:04:00 【Hydrion-Qlz】
106. Construct binary tree from middle order and post order traversal sequence
Medium difficulty
Given two arrays of integers inorder and postorder , among inorder Is the middle order traversal of a binary tree , postorder Is the post order traversal of the same tree , Please construct and return this Binary tree .
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 last one in the subsequent traversal is the current node , Its left subtree and right subtree are on its left
We can find the current node according to the subsequent 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 subsequent traversal , But you can get this value from the middle order traversal
Specific please see 37 And 38 That's ok
package cn.edu.xjtu.carlWay.tree.postAndInConstructBinaryTree;
import cn.edu.xjtu.Util.TreeNode.TreeNode;
/** * 106. Construct binary tree from middle order and post order traversal sequence * Given two arrays of integers inorder and postorder , among inorder Is the middle order traversal of a binary tree , postorder Is the post order traversal of the same tree , Please construct and return this Binary tree . * <p> * https://leetcode-cn.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ */
public class Solution {
public TreeNode buildTree(int[] inorder, int[] postorder) {
return helpBuild(inorder, 0, inorder.length, postorder, 0, postorder.length);
}
private TreeNode helpBuild(int[] inorder, int inLeft, int inRight, int[] postorder, int postLeft, int postRight) {
// There are no elements
if (inRight - inLeft < 1) {
return null;
}
// There's only one element
if (inRight - inLeft == 1) {
return new TreeNode(inorder[inLeft]);
}
// The last element in the subsequent array is the root node
int rootVal = postorder[postRight - 1];
TreeNode root = new TreeNode(rootVal);
// Find the position of the root node in the ordered array
int rootIndex = 0;
for (int i = inLeft; i < inRight; i++) {
if (inorder[i] == rootVal) {
rootIndex = i;
break;
}
}
root.left = helpBuild(inorder, inLeft, rootIndex, postorder, postLeft, postLeft + (rootIndex - inLeft));
root.right = helpBuild(inorder, rootIndex + 1, inRight, postorder, postLeft + (rootIndex - inLeft), postRight - 1);
return root;
}
}
边栏推荐
- You Li takes you to talk about C language 6 (common keywords)
- 运放电路的反馈电阻上并联一个电容是什么作用
- Pytorch parameter initialization
- Lattice coloring - matrix fast power optimized shape pressure DP
- Cnopendata list data of Chinese colleges and Universities
- Button wizard script learning - about tmall grabbing red envelopes
- 【数字IC验证快速入门】11、Verilog TestBench(VTB)入门
- Topic not received? Try this
- 贝叶斯定律
- C语言通信行程卡后台系统
猜你喜欢

Cnopendata American Golden Globe Award winning data

Few-Shot Learning && Meta Learning:小样本学习原理和Siamese网络结构(一)

Common method signatures and meanings of Iterable, collection and list

追风赶月莫停留,平芜尽处是春山

Cnopendata list data of Chinese colleges and Universities

2022 recurrent training question bank and answers of refrigeration and air conditioning equipment operation

Li Kou interview question 04.01 Path between nodes

Codeforce c.strange test and acwing

buuctf misc USB

The charm of SQL optimization! From 30248s to 0.001s
随机推荐
2022年全国最新消防设施操作员(初级消防设施操作员)模拟题及答案
A bit of knowledge - about Apple Certified MFI
Operation suggestions for today's spot Silver
Cnopendata American Golden Globe Award winning data
php导出百万数据
C language flight booking system
Why should we understand the trend of spot gold?
Few-Shot Learning && Meta Learning:小样本学习原理和Siamese网络结构(一)
力扣(LeetCode)187. 重复的DNA序列(2022.07.06)
Pytorch parameter initialization
Numbers that appear only once
Implementation of replacement function of shell script
Pytest+allure+jenkins installation problem: pytest: error: unrecognized arguments: --alluredir
The charm of SQL optimization! From 30248s to 0.001s
[VHDL parallel statement execution]
芯片资料 网站 易特创芯
2022年茶艺师(中级)考试试题及模拟考试
Qt学习26 布局管理综合实例
贝叶斯定律
padavan手动安装php