当前位置:网站首页>【二叉树】重建二叉树(先序找根,中序分治)
【二叉树】重建二叉树(先序找根,中序分治)
2022-07-20 06:36:00 【王小希ww】
【二叉树】重建二叉树(先序找根,中序分治)
题目描述
题目链接:JZ7 重建二叉树
解决思路
先序遍历:根左右; 中序遍历:左根右
通过前序序列创建根节点:
pre[preLeft]
接着在中序序列中找到根节点的位置
mid
,通过mid
划分左右子树:左子树节点个数为left_count
,右子树为right_count
;通过递归和分治思想,完成根节点的左右子树的构建。- 左子树的中序序列区间为
[vinLeft,vin_mid)
,先序序列区间为[vin_mid+1,vinRight)
; - 右子树的中序序列区间为
[preLeft + 1,preLeft + left_count + 1)
,先序序列区间为[preLeft + left_count + 1,preRight)
;
这里注意先序子序列和中序子序列的区间均是左开右闭。
- 左子树的中序序列区间为
参考代码
//AC Code
import java.util.*;
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */
public class Solution {
//在中序序列的指定区间内查找相应值的索引
public int getIndex(int []vin, int l,int r, int val){
for(int i = l; i < r; ++i){
if(vin[i] == val){
return i;
}
}
return -1;
}
//通过先序序列确定根节点,通过中序序列中根节点的位置划分left和right(区间为[vinLeft,vinRight),[preLeft,preRight)))
public TreeNode construct(TreeNode root, int []pre, int []vin, int vinLeft, int vinRight, int preLeft, int preRight){
if(vinLeft >= vinRight || preLeft >= preRight){
return null;
}
root = new TreeNode(pre[preLeft]); //先序遍历第一个值作为根节点
int vin_mid = getIndex(vin, vinLeft, vinRight, pre[preLeft]); //pre[preLeft]为根节点,获取根节点在中序序列中的索引位置
int left_count = Math.max(0,vin_mid - vinLeft); //根节点左子树节点个数
int right_count = Math.max(0,vinRight - vin_mid); //根节点右子树节点个数
root.left = construct(root.left, pre, vin, vinLeft, vin_mid, preLeft + 1, preLeft + left_count + 1); //构建左子树
root.right = construct(root.right, pre, vin, vin_mid + 1, vinRight, preLeft + left_count + 1, preRight); //构建右子树
return root;
}
public TreeNode reConstructBinaryTree(int [] pre,int [] vin) {
if(pre.length == 0){
return null;
}
TreeNode root = null;
int vinLeft = 0, vinRight = vin.length, preLeft = 0, preRight = pre.length;
root = construct(root, pre, vin, vinLeft, vinRight, preLeft, preRight);
return root;
}
}
边栏推荐
- Detailed explanation of five data types of redis
- Dynamic password lock based on stm32
- 基于STM32设计的动态密码锁
- 6 月威胁报告:新银行恶意软件 MaliBot 对移动银行用户构成威胁
- [latex] miktex+texstudio installation and configuration of thesis writing environment
- threeJS中dat.gui的使用显示文件夹点击时候及调色器
- 9 suggestions for quickly improving programming ability
- FigDraw 11. Violinplot of SCI article drawing
- 【leetcode】150 逆波兰表达式求值
- 可以快速提高编程能力的9个建议
猜你喜欢
在 IDEA 里下个五子棋不过分吧?
操作符详解—c语言
Universal query function in excel - vlookup (usage + Practice)
threeJS中dat.gui的使用显示文件夹点击时候及调色器
编程与哲学(1)
Mysql database concurrency, locking problems (shared locks and exclusive locks)
通过STM32 stlink utility工具对ST-LINK芯片信息进行读取和升级以及SWD烧录媒介
一文看懂:零代码、0代码、无代码平台是什么?怎么选?
MySQL数据库并发,上锁的问题(共享锁和排它锁)
Deep parsing of custom types
随机推荐
# CF #808 Div.2(A - C)
2022-07-19:f(i) : i的所有因子,每个因子都平方之后,累加起来。 比如f(10) = 1平方 + 2平方 + 5平方 + 10平方 = 1 + 4 + 25 + 100 = 130。
Threejst objects move at a constant speed
Learn how to choose chart types, and Xiaobai can also play with data analysis
What is the easiest explanation for SaaS? Just read this one
Three places and five centers (LDC (logical data center) unitization) and disaster recovery
【MySQL】临时表 &视图
Introduction to NC (netcat) network security tool
If:4+ iron metabolism and immune related gene markers predict clinical outcomes and molecular characteristics of triple negative breast cancer
RNA 23. Risk factor association diagram of Cox model of expressed genes in SCI articles (ggrisk)
Wangeditor uncaught (in promise) error: the editor instance already exists in the initialization node, and the edit cannot be created repeatedly
[MySQL] temporary table & View
Storage of integer and floating point numbers in memory
The latest upx3.91 supports win64 / PE plus / minus shell
Four questions, judge whether you are suitable for learning programming
Super dry goods: design summary, tools and technical points of data visualization are all available
9 suggestions for quickly improving programming ability
Detailed explanation of five data types of redis
Ibatis and SQL injection
Command line parameters of C language programming skills