1156. Binary tree

Description

Your task is very simple: Given a binary tree, every node of which contains one upper case character (‘A’ to ‘Z’); you just need to print all characters of this tree in pre-order.

Input

Input may contain several test data sets.

      For each test data set, first comes one integer n (1 <= n <= 1000) in one line representing the number of nodes in the tree. Then n lines follow, each of them contains information of one tree node. One line consist of four members in order: i (integer, represents the identifier of this node, 1 <= i <= 1000, unique in this test data set), c (char, represents the content of this node described as above, ‘A’ <= c <= ‘Z’), l (integer, represents the identifier of the left child of this node, 0 <=  l <= 1000, note that when l is 0 it means that there is no left child of this node), r (integer, represents the identifier of the right child of this node, 0 <=  r <= 1000, note that when r is 0 it means that there is no right child of this node). These four members are separated by one space.

      Input is ended by EOF.

      You can assume that all inputs are valid. All nodes can form only one valid binary tree in every test data set.

Output

For every test data set, please traverse the given tree and print the content of each node in pre-order. Characters should be printed in one line without any separating space.

Sample Input

Copy sample input to clipboard
34 C 1 31 A 0 03 B 0 011000 Z 0 031 Q 0 22 W 3 03 Q 0 0

Sample Output

CABZQWQ

#include <bits/stdc++.h>
using namespace std;

typedef struct node
{
	char value;
	int l,r;	
}node;

node nod[1005];
bool isroot[1005];
int inde[1005];
int n;
void preorder(node  root)
{
	cout<<root.value;
	if(root.l!=0) 
	{
		preorder(nod[root.l]);
	}
	if(root.r!=0) 
	{
		preorder(nod[root.r]);
	}
}
int main()  
{  
	while(cin>>n)
	{
		memset(isroot,1,sizeof(isroot));
		for(int i=0;i<n;i++)
		{
			int tem;
			cin>>tem;
			inde[i]=tem;
			cin>>nod[tem].value>>nod[tem].l>>nod[tem].r ;
			
			isroot[nod[tem].r ]=0;
			isroot[nod[tem].l ]=0;
		}		
		for(int i=0;i<n;i++)
		{
			if(isroot[inde[i]])
			{
				preorder(nod[inde[i]]);
			}
		}
		cout<<endl;
	}
	return 0;  
}  
 

这道题用inde数组记录每个结点的编号。通过inde数组去访问isroot数组,跟nod数组。

不是每一道跟树有关的题都要建树,可以通过数组转换。


### `void preorder(binarytree tree)` 函数实现思路 前序遍历(Preorder Traversal)是一种二叉树的遍历方式,其遍历顺序为:根节点 -> 左子树 -> 右子树。`void preorder(binarytree tree)` 函数的目的就是实现对二叉树的前序遍历。 ### 递归实现 以下是使用递归方式实现 `void preorder(binarytree tree)` 函数的示例代码: ```python class BinaryTree: def __init__(self, root_val): self.key = root_val self.leftChild = None self.rightChild = None def preorder(self): print(self.key) if self.leftChild: self.leftChild.preorder() if self.rightChild: self.rightChild.preorder() # 使用示例 if __name__ == "__main__": # 创建一个简单的二叉树 root = BinaryTree(1) root.leftChild = BinaryTree(2) root.rightChild = BinaryTree(3) root.leftChild.leftChild = BinaryTree(4) root.leftChild.rightChild = BinaryTree(5) # 调用前序遍历函数 root.preorder() ``` 上述代码定义了一个 `BinaryTree` 类,其中的 `preorder` 方法实现了前序遍历。在 `preorder` 方法中,首先打印当前节点的值,然后递归地调用 `preorder` 方法遍历左子树和右子树。 ### 非递归实现 也可以使用栈来实现非递归的前序遍历,以下是示例代码: ```python class BinaryTree: def __init__(self, val): self.val = val self.left = None self.right = None def preorder(root): if not root: return stack = [root] while stack: node = stack.pop() print(node.val) if node.right: stack.append(node.right) if node.left: stack.append(node.left) # 使用示例 if __name__ == "__main__": # 创建一个简单的二叉树 root = BinaryTree(1) root.left = BinaryTree(2) root.right = BinaryTree(3) root.left.left = BinaryTree(4) root.left.right = BinaryTree(5) # 调用前序遍历函数 preorder(root) ``` 在非递归实现中,使用一个栈来模拟递归调用的过程。首先将根节点压入栈中,然后循环取出栈顶节点进行访问,并将其右子节点和左子节点依次压入栈中。 ### 使用方法 - **创建二叉树**:首先需要创建一个二叉树的实例,可以根据实际需求构建不同结构的二叉树。 - **调用 `preorder` 函数**:将创建好的二叉树的根节点作为参数传递给 `preorder` 函数,即可完成前序遍历并输出结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值