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中阻塞队列的使用

    http://blog.csdn.net/qq_35101189/article/details/56008342 在新增的Concurrent包中,BlockingQueue很好的解决了多线程中,如 ...

  2. VMWare关闭beep声

    在虚拟机文件夹下找到 .vmx 文件,在文件末尾添加 mks.noBeep = "TRUE" ,重启虚拟机即可.

  3. jQuery实现轮播切换以及将其封装成插件(3)

    在前两篇博文中,我们写了一个普通的轮播切换.但是我们不能每一次需要这个功能就把这些代码有重新敲一次.下面我们就将它封装成一个插件. 至于什么是插件,又为什么要封装插件,不是本文考虑的内容.  我们趁着 ...

  4. 头部导航悬浮,css

    .header{ position:fixed; z-index:100; left:; right:; } 如图.

  5. 电话状态监听 - iOS

    今天接到一个监听状态的需求,当使用 App 时若电话介入需要对当前状态进行监听操作(注:并非通话内容),根据不同的状态实行相关的需求操作,废话不多说步骤如下. 首先,常规操作先引用对应的头文件,来为后 ...

  6. socket传送二进制流的一些总结

    第一次实质性的接触socket通信方面的工作,所以遇到的问题还真不少,写篇博客记录一下,提升下记忆. 需求是通过私有协议进行二进制数据的传输,必须保证数据包不能被丢失,所以选择tcp的socket进行 ...

  7. PXE自动化安装CentOS6/7

    服务器为centos7 安装前准备:关闭防火墙和SELINUX 虚拟机准备第二块网卡,设置主机模式,关闭虚拟机网络配置中主机模式的DHCP功能,并设置静态IP nmcli c a con-name e ...

  8. JDK1.8简单配置环境变量---两步曲

    鄙人最近重新装完系统之后,在安装和配置jdk1.8的时候,发现网上许多教程配置jdk环境变量时都还在沿用传统的方式配置,但是随着技术的更新,完全没有必要那么麻烦了. 下载和安装jdk的教程,在这里就不 ...

  9. webmin纯web界面管理linux系统

    关键字: 摘要:从Windows环境的管理转到Linux环境的管理时所面临的挑战之一是,您需要去学习利用新的工具.作为一个管理员,您希望理解操作系统的细节以发挥它的最大功效.但是,当您还处在学习阶段时 ...

  10. php-5.6.26源代码 - opcode列表

    文件 php-5.6.26/Zend/zend_vm_opcodes.h #ifndef ZEND_VM_OPCODES_H #define ZEND_VM_OPCODES_H BEGIN_EXTER ...