POJ 1577 Falling Leaves 二叉树题解

给出按最底层叶子节点到根节点的数据,然后要求重建树,前序输出最终建的树。

都是两个基本操作解决:

1 二叉树插入操作

2 前序遍历

简单题目了。

#include <stdio.h>
#include <string>
#include <algorithm>
#include <vector>
using std::vector;
using std::string;

const int MAX_B = 1024;
char buf[MAX_B];
int id = 0, len = 0;

inline char getFromBuf()
{
	if (id >= len)
	{
		len = fread(buf, 1, MAX_B, stdin);
		id = 0;
	}
	return buf[id++];
}

void getStrFromBuf(string &n)
{
	char a = getFromBuf();
	while ((a == ' ' || a == '\n') && len) a = getFromBuf();

	n.clear();
	while ((a != ' ' && a != '\n') && len)//老是写&&,错成||
	{
		n.push_back(a);
		a = getFromBuf();
	}
}

struct Node
{
	char alpha;
	Node *left, *right;
	explicit Node (char a = ' ') : alpha(a), left(NULL), right(NULL) {}
};

Node *insertNode(Node *root, char a)
{
	if (!root) return new Node(a);

	if (a < root->alpha) root->left = insertNode(root->left, a);
	else root->right = insertNode(root->right, a);
	return root;
}

void printTree(Node *r)
{
	if (r)
	{
		putchar(r->alpha);
		printTree(r->left);
		printTree(r->right);
	}
}

void releaseTree(Node *r)
{
	if (r)
	{
		releaseTree(r->left);
		releaseTree(r->right);
		delete r; r = NULL;
	}
}

int main()
{
	string s;
	while (true)
	{
		getStrFromBuf(s);
		if (len == 0 || s == "$") break;

		vector<string> vstr;
		while (s != "*" && s != "$")
		{
			vstr.push_back(s);
			getStrFromBuf(s);
		}
		Node *root = NULL;
		for (int i = (int)vstr.size() - 1; i >= 0 ; i--)
		{
			for (int j = 0; j < (int)vstr[i].size(); j++)
			{
				root = insertNode(root, vstr[i][j]);
			}
		}
		printTree(root);
		releaseTree(root);
		putchar('\n');
	}
	return 0;
}

POJ 1577 Falling Leaves 二叉树题解,布布扣,bubuko.com

时间: 2024-10-10 16:13:02

POJ 1577 Falling Leaves 二叉树题解的相关文章

POJ 1577 Falling Leaves 二叉树操作

Description Figure 1 Figure 1 shows a graphical representation of a binary tree of letters. People familiar with binary trees can skip over the definitions of a binary tree of letters, leaves of a binary tree, and a binary search tree of letters, and

POJ 1577 Falling Leaves 二叉搜索树

HDU 3791 Falling Leaves 二叉搜索树  Figure 1Figure 1 shows a graphical representation of a binary tree of letters. People familiar with binary trees can skip over the definitions of a binary tree of letters, leaves of a binary tree, and a binary search tr

POJ 1577 Falling Leaves

题意:给出一些字符串,从上到下的建树,输出其前序遍历 像前面那一题一样,先建树,然后再递归前序遍历 不过想像上一题那样用数组建树,建树和上题一样的办法,可是应该怎么输出前序遍历呢= = 还是看的题解= = 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <cmath> 5 #include<algorithm> 6 using namespace std

UVA 399 The Falling Leaves(二叉树)

 The Falling Leaves  Each year, fall in the North Central region is accompanied by the brilliant colors of the leaves on the trees, followed quickly by the falling leaves accumulating under the trees. If the same thing happened to binary trees, how l

UVA 699 The Falling Leaves (二叉树水题)

本文纯属原创,转载请注明出处,谢谢.http://blog.csdn.net/zip_fan. Description Each year, fall in the North Central region is accompanied by the brilliant colors of the leaves on the trees, followed quickly by the falling leaves accumulating under the trees. If the sam

【数据结构】The Falling Leaves(6-10)

[UVA699]The Falling Leaves 算法入门经典第6章例题6-10(P159) 题目大意:有一颗二叉树,求水平位置的和. 试题分析:乱搞就可以过,将树根节点的pos记为0,向左-1,向右+1,统计答案即可. #include<iostream> #include<cstring> #include<cstdio> #include<vector> #include<queue> #include<stack> #in

UVa 699.The Falling Leaves【7月23】

The Falling Leaves Each year, fall in the North Central region is accompanied by the brilliant colors of the leaves on the trees, followed quickly by the falling leaves accumulating under the trees. If the same thing happened to binary trees, how lar

UVA 699 The Falling Leaves

#include<cstdio> #include<iostream> #include<queue> #include<cstring> #include<string> #include<math.h> #include<stack> #include<cstdlib> #include<map> #include<algorithm> #include<cctype>

UVa 699 The Falling Leaves(递归建树)

题意  假设一棵二叉树也会落叶  而且叶子只会垂直下落   每个节点保存的值为那个节点上的叶子数   求所有叶子全部下落后   地面从左到右每堆有多少片叶子 和上一题有点像  都是递归输入的  一个节点(设水平位置为p)  则它的左右儿子节点的水平位置分别为  p-1  p+1   也是可以边输入边处理的  输入完也就得到答案了   注意每个样例后面都有一个空行  包括最后一个 #include<cstdio> #include<cstring> using namespace s