All rights reserved by DianeSoHungry (Qingyun Hu).

Original URL: https://www.cnblogs.com/DianeSoHungry/p/8891148.html

Contents

  • Construct a binary tree from DLR and LDR traversals.

Problem 1

Construct a binary tree from DLR and LDR traversals.

(根据先序遍历和中序遍历序列,建二叉树)

Example:

Input:

8
1 2 4 7 3 5 6 8
4 7 2 1 5 3 8 6

Output:

Head pointer of the built binary tree

Intuition

For each DLR, you know the first element correspond to the root node. By finding the first element of DLR in LDR, you can split out the subarray of DLR and LDR for left subtree, so as to the subarray of DLR and LDR for right subtree. That is, time for recursion.

Solution in CPP

#include<iostream>
#include<queue>
struct BtNode{
int val;
BtNode* left;
BtNode* right;
BtNode(int val): val(val), left(nullptr), right(nullptr){ };
};
BtNode* BuildBT(int pre[], int mid[], int len){
if (len == 0) {
return nullptr;
}
BtNode* root;
for (int i = 0; i < len; ++i) {
if ( mid[i] == pre[0]) {
root = new BtNode(mid[i]);
root->left = BuildBT(pre+1, mid, i);
root->right = BuildBT(pre+i+1, mid+i+1, len-i-1);
break;
}
} return root;
}
void TraverseLevelwise(BtNode* bt){
std::queue<BtNode*> q;
q.push(bt);
while (!q.empty()) {
if (q.front() != nullptr) {
std::cout << (q.front()->val) << " ";
q.push(q.front()->left);
q.push(q.front()->right);
}
else {
// std::cout << "null ";
} q.pop();
}
std::cout << std::endl;
} int main(){
int n;
std::cin >> n;
int preorder[n];
int midorder[n];
for (int i = 0; i < n; ++i) {
std::cin >> preorder[i];
}
for (int i = 0; i < n; ++i) {
std::cin >> midorder[i];
}
BtNode* res = BuildBT(preorder, midorder, n);
TraverseLevelwise(res);
return 0;
}

After getting the binary tree, for checking, the level order of the tree is printed as below.

1 2 3 4 5 6 7 8

Reference

《剑指offer》

Ubuntu下禁用笔记本自带键盘的更多相关文章

  1. ubuntu禁用笔记本自带键盘

    ubuntu如何禁用笔记本键盘 打开终端运行命令 xinput list Virtual core pointer id=2 [master pointer (3)] ⎜ ↳ Virtual core ...

  2. deepin禁用笔记本自带键盘

    参考命令: sudo apt install xinput xinput xinput list-props 'AT Translated Set 2 keyboard' xinput set-pro ...

  3. Linux下禁用笔记本触摸板

    1 概述 在Linux下禁用触摸板的方法有很多,这里列举三种: 图形界面配置关闭 modprobe关闭 xinput关闭 2 图形界面配置关闭 笔者的环境为Manjaro+Xfce,其他的桌面也应该类 ...

  4. 在ubuntu下关闭笔记本触摸板

    http://www.cnblogs.com/icejoywoo/archive/2011/04/14/2016318.html 原文地址:http://forum.ubuntu.org.cn/vie ...

  5. ubuntu下禁用和恢复触摸板

    1.一般禁用选项在 settings > mouse and touchpad 中.(16.04通过实验)如果无法禁用或者希望恢复,向下看. 2.命令行键入: xinput ,插卡touchpa ...

  6. centos系统下禁用笔记本触控板

    最近把零几年的老爷笔记本拿出来用,使用windows系统实在太卡了,于是折腾安装上Centos系统了,但是在使用的过程中发现鼠标经常失效.使用了多种方法(比如:http://blog.csdn.net ...

  7. linux 关闭笔记本自带键盘

    linux 命令行工具 xinput list 找到 AT Translated Set 2 keyboard,其 id为 13 设置值为 0 xinput 如果想恢复,对应的值设为1即可 xinpu ...

  8. Ubuntu下如何禁用IPv6

    Ubuntu下如何禁用IPv6 2013-10-16 11:32:02 分类: HADOOP      分布式下的hadoop/hbase运行总出问题,zookeeper连接总是出问题,怀疑可能是ip ...

  9. 一个禁用mac内置键盘的方法

    一个禁用mac内置键盘的方法 强大的 karabiner, 非常好用. 可以直接在有外接键盘连接的情况下, 禁用掉内置键盘 另外一个方法是启用mac的 鼠标键, 感觉用处不是很大, 修饰健并没有被禁用 ...

随机推荐

  1. Java-笔记1

    /* 对第一个java程序进行总结 1. java程序编写-编译-运行的过程 编写:我们将编写的java代码保存在以".java"结尾的源文件中 编译:使用javac.exe命令编 ...

  2. 如何在Linux中显示和设置主机名

    原文链接 随着连接到网络的计算机数量越来越多,每一台计算机都需要有一个属性来区别于其它计算机.和现实世界中的人一样,计算机也有一个叫做hostname(主机名)的属性. 什么是hostname 从它的 ...

  3. 《JavaScript高级程序设计第三版》——细碎知识痛点整理(第六章)

    面向对象的程序设计 对象是一组没有特定顺序的值6.1.1 属性类型ECMAScript中有两种属性:数据属性和访问器属性.1. 数据属性Configurable 表示能否通过delete删除属性从而重 ...

  4. 5.Spring Cloud初相识-------Hystrix熔断器

    前言: 1.介绍Hystrix 在一个分布式系统里,许多依赖不可避免的会调用失败,比如超时.异常等,如何能够保证在一个依赖出问题的情况下,不会导致整体服务失败,这个就是Hystrix需要做的事情.Hy ...

  5. singnal 13 was raised

    在app运行过程中按下home键或者其他原因app被挂起,socket连接不会断开,服务器为了节省资源,在一段时间后会主动关闭这个连接.当玩家再次切回到游戏后,前端并不知道这个连接已经断开了,继续通过 ...

  6. Oracle多表连接方法

    笛卡尔连接[结果集为各表记录的乘积] SELECTt * FROM table_1, table_2, table_n SELECTt * FROM table_1 CROSS JOIN table_ ...

  7. 【赛时总结】 ◇赛时·I◇ AtCoder ARC-098

    ◆赛时I◆ ARC-098 ■试题&解析■ ◆本场最水◆ C-Attention 长点儿信心吧-- [AtCoder ARC-098 C] [解析] 既然只存在左右(东西)两个朝向,那么领导右 ...

  8. mysql5.6 配置 文件

    mysql 3306 主库配置文件 [client] port = 3306 default-character-set=utf8mb4 socket = /ssd/mysql/3306/tmp/my ...

  9. js | javascript实现浏览器窗口大小被改变时触发事件的方法

    转载 当浏览器的窗口大小被改变时触发的事件window.onresize 为事件指定代码: 代码如下: window.onresize = function(){ } 例如: 浏览器可见区域信息: 代 ...

  10. hibernate映射实体类查询时数据库空字段赋值给实体类报错的问题

    因为一直报实体类空异常,去网上查了资料只查到了并没有查到数据库空值时不给实体类赋值的属性 异常 org.hibernate.InvalidMappingException: Could not par ...