Sicily-1156

时间:2023-12-25 17:49:07

一.      模仿树的的先序遍历。范围是1000个节点。用数组存储节点的信息。

二.      要注意的是,头结点是不确定的,所以在前序遍历之前要找出头结点,除了头结点的下标值出现一次之外,其他结点的下标值都会出现两次,根据这个特征可以利用异或运算(^),算出头结点。

三.      源码

 //
// main.cpp
// sicily-1156
//
// Created by ashley on 14-10-12.
// Copyright (c) 2014年 ashley. All rights reserved.
// #include <iostream>
using namespace std;
typedef struct
{
char element;
int leftChild;
int rightChild;
}node;
node binaryTree[];
void preorder(int front)
{
cout << binaryTree[front].element;
if (binaryTree[front].leftChild != ) {
preorder(binaryTree[front].leftChild);
}
if (binaryTree[front].rightChild != ) {
preorder(binaryTree[front].rightChild);
}
}
int main(int argc, const char * argv[])
{
int counts = ;
char ele;
int number;
int left, right;
while (cin >> counts) {
int source = ;
for (int i = ; i < counts; i++) {
cin >> number >> ele >> left >> right;
source = source ^ left ^ right ^ number;
binaryTree[number] = node{ele, left, right};
}
preorder(source);
cout << endl;
}
return ;
}

相关文章