二叉树垂直遍历 java_【004】二叉树垂直遍历

二叉树垂直遍历

题目描述

输入输出

示例输入

实例输出

DFS

BFS

更简单的方法

二叉树垂直遍历

题目描述

对于一个二叉树,输出它的垂直遍历结果;对于同一列的节点,按照从左向右,从上向下的顺序排列。

例如,对于以下二叉树:

1

/ \

2 3

/

4

垂直遍历的结果是:2 1 4 3

输入输出

输入

- 第一行是n,表示节点个数(节点编号从0到n-1);当n=-1时,表示输入结束

- 之后的n行,每一行有三个整数,分别表示:节点的数值,左子树的编号,右子树的编号(编号-1表示节点为空)

输出

- 针对每组输入,输出垂直遍历的结果

示例输入

4

1 1 2

2 -1 -1

3 3 -1

4 -1 -1

-1

实例输出

2 1 4 3

DFS

#include

#include

#include

#include

#include

#include

using namespace std;

struct Node {

int data;

Node *left;

Node *right;

};

class TreePrint {

private:

map > nmap;

public:

void dfs(Node *root, int pos) {

if (root == NULL) return;

nmap[pos].push_back(root->data);

dfs(root->left, pos-1);

dfs(root->right, pos+1);

}

void display() {

int min=0;

while (nmap.find(min) != nmap.end()) --min;

for (int i=min+1; nmap.find(i) != nmap.end(); ++i)

for (vector::iterator it = nmap[i].begin();

it != nmap[i].end(); ++it)

printf("%d ", *it);

printf("\n");

}

};

int main() {

ifstream in("../input.txt");

int n;

in >> n;

while (n != -1) {

Node *tree = new Node[n];

for (int i=0; iint tmp, left, right;

in >> tmp >> left >> right;

tree[i].data = tmp;

if (left != -1) tree[i].left = &tree[left];

else tree[i].left = NULL;

if (right != -1) tree[i].right = &tree[right];

else tree[i].right = NULL;

}

TreePrint tp;

tp.dfs(&tree[0], 0);

tp.display();

in >> n;

}

in.close();

return 0;

}

然而这个结果并不正确,不能妥善的处理孩子节点超过父节点的深度的情况。

BFS

#include

#include

#include

#include

#include

#include

using namespace std;

struct Node {

int data;

Node *left;

Node *right;

};

class TreePrint {

private:

map > nmap;

public:

void dfs(Node *root, int pos) {

if (root == NULL) return;

nmap[pos].push_back(root->data);

dfs(root->left, pos-1);

dfs(root->right, pos+1);

}

void bfs(Node *root) {

queue q;

queue qpos;

q.push(root);

qpos.push(0);

while (!q.empty()) {

Node *tmp = q.front();

int pos = qpos.front();

q.pop();

qpos.pop();

nmap[pos].push_back(tmp->data);

if (tmp->left != NULL) {

q.push(tmp->left);

qpos.push(pos-1);

}

if (tmp->right != NULL) {

q.push(tmp->right);

qpos.push(pos+1);

}

}

}

void display() {

int min=0;

while (nmap.find(min) != nmap.end()) --min;

for (int i=min+1; nmap.find(i) != nmap.end(); ++i)

for (vector::iterator it = nmap[i].begin();

it != nmap[i].end(); ++it)

printf("%d ", *it);

printf("\n");

}

};

int main() {

ifstream in("../input.txt");

int n;

in >> n;

while (n != -1) {

Node *tree = new Node[n];

for (int i=0; iint tmp, left, right;

in >> tmp >> left >> right;

tree[i].data = tmp;

if (left != -1) tree[i].left = &tree[left];

else tree[i].left = NULL;

if (right != -1) tree[i].right = &tree[right];

else tree[i].right = NULL;

}

TreePrint tp;

tp.bfs(&tree[0]);

tp.display();

in >> n;

}

in.close();

return 0;

}

更简单的方法

由于输入的时候就是BFS遍历,所以输入的时候就可以进行排序

#include

#include

#include

#include

using namespace std;

struct Node {

int data;

int id;

int colum;

};

bool compareTo(Node &l, Node &r) {

if (l.colum == r.colum) return l.id < r.id;

else return l.colum < r.colum;

}

int main() {

ifstream in("../input.txt");

int n;

in >> n;

while (n != -1) {

queue q;

q.push(0);

Node *tree = new Node[n];

tree[0].colum = 0;

for (int i=0; iint tmp, left, right;

in >> tmp >> left >> right;

tree[i].data = tmp;

tree[i].id = i;

int pos = q.front();

q.pop();

if (left != -1) {

tree[left].colum = pos - 1;

q.push(pos-1);

}

if (right != -1) {

tree[right].colum = pos + 1;

q.push(pos+1);

}

}

sort(tree, tree+n, &compareTo);

for (int i=0; iprintf("%d ", tree[i].data);

}

printf("\n");

in >> n;

}

in.close();

return 0;

}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/427173.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

matlab二元一次方程求解_2-函数的求解计算

一、本期介绍函数的求解有两种&#xff08;1&#xff09;已知x求y&#xff08;2&#xff09;已知y求x1.1已知x求y回想我们上一期&#xff0c;讲了多项式求解的方法&#xff0c;也是已知x求y。想一下&#xff0c;多项式不也是函数的一种吗&#xff0c;所以本期求解的方法同样适用…

linux lnmp yum版安装

LAMP&#xff08;linux、apache、mysql、php&#xff09;&#xff0c;是四个套件的合成&#xff0c;简单讲就是要把php运行在linux上&#xff0c;需要依赖apache和mysql数据库。 1 准备好一个linux系统&#xff08;centos7&#xff09;   确保selinux、firewall已经关闭   …

面向对象的四大特征

面向对象的四大特征 面向对象的程序设计方法要求语言必须具备抽象、封装、继承和多态性这几个关键要素。 面向对象程序设计&#xff0c;是通过为数据和代码建立分块的内存区域&#xff0c;以便提供对程序进行模块化的一种程序设计方法。对象是计算机内存中的一块区域&#xff0…

python装饰器调用顺序_聊一聊Python装饰器的代码执行顺序

为什么写这篇文章&#xff1f;起因是QQ群里边有人提了一个问题&#xff1a;之前导入模块只需要1~2秒&#xff0c;为什么现在变成需要2~3分钟&#xff1f;我的第一感觉是&#xff1a;是不是导入的模块顶层代码里边&#xff0c;做了什么耗时的事情。隔了一天&#xff0c;他的问题…

centos7 安装cacti

1 cacti运行环境准备   cacti需要phpapachemysqlsnmpRRDTool&#xff0c;以及cacti本身。cacti本体是用php开发的网站&#xff0c;通过snmp对远端设备信息进行采集。apachemysqlphp在以前已经做过了      这里只对剩余的部分进行安装。 2 安装snmp    yum install -y n…

python第三方库-基础

1.python社区 python有一个全球社区&#xff0c;提供了超过十三万个涵盖各种领域应用的第三方库&#xff0c;该社区可通过 http://pypi.org/ 来访问。PyPI&#xff08;Python Package Index&#xff09;是python包的索引&#xff0c;学会检索并利用PyPI&#xff0c;找到合适的第…

python折线图matplotlib库_Python如何使用内置库matplotlib绘制折线图

这篇文章主要介绍了Python如何使用内置库matplotlib绘制折线图,文中通过示例代码介绍的非常详细&#xff0c;对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下环境准备&#xff1a;需要安装matplotlib&#xff0c;安装方式&#xff1a;pip install matplotlib…

linux下zabbix安装

1本人用的是apachemysqlphp 2下载zabbix软件包&#xff0c;官网下载 https://sourceforge.net/projects/zabbix/files/ZABBIX Latest Stable/2.2.23/zabbix-2.2.23.tar.gz/download 上传到 var/www/html下 3cd /var/www/html #进入软件包下载目录 tar zxvf zabbix-2.2.23.tar.g…

java cas机制_Java CAS机制详解

CAS目的&#xff1a;在多线程中为了保持数据的准确性&#xff0c;避免多个线程同时操作某个变量&#xff0c;很多情况下利用关键字synchronized实现同步锁&#xff0c;使用synchronized关键字修可以使操作的线程排队等待运行&#xff0c;可以说是一种悲观策略&#xff0c;认为线…

「一本通 4.1 练习 2」简单题

题目描述 题目来源&#xff1a;CQOI 2006 有一个 n 个元素的数组&#xff0c;每个元素初始均为 0。有 m 条指令&#xff0c;要么让其中一段连续序列数字反转——0 变 1&#xff0c;1变 0&#xff08;操作 1&#xff09;&#xff0c;要么询问某个元素的值&#xff08;操作 2&…

定时器取数据时实时进来的数据_Redis-数据淘汰策略amp;持久化方式(RDB/AOF)amp;Redis与Memcached区别...

Redis与Memcached区别&#xff1a; 两者都是非关系型数据库。主要有以下不同&#xff1a; 数据类型&#xff1a; Memcached仅支持字符串类型。 redis支持&#xff1a;String,List,set,zset,hash 可以灵活的解决问题。 数据持久化&#xff1a; Memcached不支持持久化。 Redis采…

linux 下建立多个tomcat

第一步&#xff1a;复制&#xff0c;解压 将准备好的tomcat压缩包复制到你准备安装的目录&#xff0c;我的tomcat压缩包名字是tomcat.tar.gz,我的安 装目录是 /usr/java/tomcat 第二步&#xff1a;解压tomcat [rootaliServer tomcat]# tar -xvf tomcat.tar.gz 第三步&#xff…

java apply 函数_Js(Javascript)中的apply方法的使用

Function.apply(obj,args)方法能接收两个参数&#xff0c;简单说apply方法作用就是给类或方法中的this赋值。所以学会这个方法首先要知道this的作用。(this的用法可以看一下这个链接&#xff1a;http://www.cjavapy.com/article/8/ )obj&#xff1a;这个对象将代替Function类里…

linux iptables配置

1 iptables默认系统自带 setup 2重启防火墙 /etc/init.d/iptables restart 3接受端口 Vi /etc/sysconfig/iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT 4 #配置&#xff0c;禁止进&#xff0c;允许出&#xff0c;允许回环网卡 iptables -P I…

memcpy函数_[PART][BUG][MSVCRT][C][CCF NOI1097] 关于memcpy的坑

[Incompleted]CCF NOI1097 试题&#xff0c;本人的源码&#xff1a;Ubuntu Pastebin​paste.ubuntu.comUbuntu PastebinUbuntu Pastebin: SourceCodebyJulianDroid​paste.ubuntu.com满分代码&#xff1a;https://blog.csdn.net/tigerisland45/article/details/71038551​blog.…

Bugku杂项-convert

一进去就发现一堆二进制数&#xff0c;然后考虑怎么才能把这个和隐写扯上关系。首先&#xff0c;二进制我们肉眼就是看不懂再说什么的&#xff0c;这里就想到了转换&#xff0c;再联想上hex将原始数据转化为16进制。我们可以先把2进制转化为16进制&#xff0c;然后再放到hex上看…

tomcat:Cannot find /usr/local/tomcat1/bin/setclasspath.sh

首先看下报错代码&#xff1a; Cannot find /usr/local/tomcat1/bin/setclasspath.sh This file is needed to run this program这个可能是没有在 /etc/profile 中配置环境&#xff0c;这是第一种可能&#xff1b;如果是这种情况的话&#xff0c;可以这样做:vi /etc/profile 并…

在java中柱状图代码_我在java中编写了个柱状图,可运行了,我想让柱状图在JSP页面中显示,请问有什么方法么?谢谢。...

h1,h2 代表了柱形图的高度你可以这样试一试function createImgItem(count){var divdocument.createElement("");var imgdocument.createElement("");img.src"getCertReviewInfoImg.jspx?perCertId${perCertId}&reviewIndex"count;div.appen…

图解cacti简单使用

1登录 admin admin 2点击devices localhost 3进入配置保存 4保存 http服务要启动哦 5一步步做 6graph tree 7执行/usr/bin/php /var/www/html/cacti/poller.php 8如果时间设置错误去php.ini里面修改时间 YSTEM STATS: Time:0.4759 Method:cmd.php Processes:1 Threads:N/…

AFNetworking 3.0源码阅读 - AFURLResponseSerialization

这次来说一下AFURLResponseSerialization这个HTTP响应类。 定义了一个协议&#xff0c;该协议返回序列化后的结果。后续的AFHTTPResponseSerializer以及他的子类都遵循了该协议 该类内有很多子类定义&#xff0c;这里借用一张图来展示&#xff0c;之后一个一个来说。 我们先来看…