二叉树垂直遍历 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;

}

你可能感兴趣的:(二叉树垂直遍历,java)