当前位置:网站首页>Come n times - 07. Rebuild the binary tree
Come n times - 07. Rebuild the binary tree
2022-07-31 09:20:00 【Qin feather】
专栏前言:
本专栏主要是算法训练,目的很简单.在掌握基本的java知识后,学习最重要的算法知识,在学习之前首先要对自身有一定的理解,如果不知道怎么做欢迎来私聊.
算法的过程很枯燥,但是也很特别,不断地刷题,不断地分享才会越来越好,给别人讲明白才是真正学会了.在分享中学会知识.
坚持就是胜利~~~
题目描述:
输入某二叉树的前序遍历和中序遍历的结果,请构建该二叉树并返回其根节点.
假设输入的前序遍历和中序遍历的结果中都不含重复的数字.
示例1:
Input: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
Output: [3,9,20,null,null,15,7]
示例2:
Input: preorder = [-1], inorder = [-1]
Output: [-1]
限制:
0 <= 节点个数 <= 5000
解题1:
I have written my thoughts on this question below,more practice
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */
class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
if(preorder == null || preorder.length == 0){
return null;
}
//根节点
TreeNode root = new TreeNode(preorder[0]);
Deque<TreeNode> stack = new LinkedList<>();
//Push the root node onto the stack
stack.push(root);
//Define an inorder traversal subscript
int inorderIndex = 0;
//循环前序遍历
for(int i = 1; i < preorder.length; ++i){
//The value of the preorder traversal
int preorderVal = preorder[i];
//Find the top value of the stack
TreeNode node = stack.peek();
//If the value at the top of the stack is not equal The first value of the inorder traversal
if(node.val != inorder[inorderIndex]){
//Then the value is the left child of the root node,Push the left child onto the stack
node.left = new TreeNode(preorderVal);
stack.push(node.left);
}else{
while(!stack.isEmpty() && stack.peek().val == inorder[inorderIndex]){
node = stack.pop();
inorderIndex++;
}
node.right = new TreeNode(preorderVal);
stack.push(node.right);
}
}
return root;
}
}
课后习题:
序号 | 题目链接 | 难度评级 |
---|---|---|
1 | 105. 从前序与中序遍历序列构造二叉树 | 中等 |
边栏推荐
- loadrunner-controller-目标场景Schedule配置
- JSP exception对象简介说明
- MySQL (2)
- 来n遍剑指--09. 用两个栈实现队列
- js implements the 2020 New Year's Day countdown bulletin board
- 内联元素居中
- 傅里叶变换,拉普拉斯变换学习记录
- 02 Truffle TutorialToken 示例
- I advise those juniors and juniors who have just started working: If you want to enter a big factory, you must master these core skills!Complete Learning Route!
- loadrunner-Controller负载测试-各模块功能记录01测试场景设计
猜你喜欢
来n遍剑指--06. 从尾到头打印链表
基于学生成绩管理系统(附源代码及数据库)
Hematemesis summarizes thirteen experiences to help you create more suitable MySQL indexes
Are postgresql range queries faster than index queries?
Spark 在 Yarn 上运行 Spark 应用程序
【机器学习】用特征量重要度(feature importance)解释模型靠谱么?怎么才能算出更靠谱的重要度?
优信年营收16亿:亏损3亿 已与蔚来资本及58集团签署股权协议
Define event types in Splunk Web
来n遍剑指--05. 替换空格
A Spark SQL online problem troubleshooting and positioning
随机推荐
混合型界面:对话式UI的未来
js部门预算和支出雷达图
js department budget and expenditure radar chart
MySQL 视图(详解)
&#x开头的是什么编码?
数字加分隔符
湖仓一体电商项目(二):项目使用技术及版本和基础环境准备
spark filter
Flink1.15源码阅读flink-clients——flink命令行帮助命令
ONES 入选 CFS 财经峰会「2022数字化创新引领奖」
JSP response,request操作中(中文乱码)-如何解决呢?
02 Truffle TutorialToken 示例
ARC在编译和运行做了什么?
射频电路学习之滤波电路
零代码工具推荐 八爪鱼采集器
优信年营收16亿:亏损3亿 已与蔚来资本及58集团签署股权协议
vue element form表单规则校验 点击提交后直接报数据库错误,没有显示错误信息
JSP page对象简介说明
【问题记录】TypeError: eval() arg 1 must be a string, bytes or code object
(selenium)Service geckodriver unexpectedly exited. Status code was: 64