1156. Binary tree root finding method with two forks

Source: Internet
Author: User

#include "iostream"
#include "Memory.h"
using namespace Std;


/*
Find the Root method:
Number of inputs per node: The root node is entered once when it enters itself,
A non-root node is entered once when the parent node is entered, and then entered when you enter
Once, so I entered it two times. So you can calculate each node through the counter
The number of times it was entered to find the root node.
*/
const int MAX = 1002;
struct node{
int id;
Char Val;
int left, right;
Node (char c, int i = 0) {
id = i;
val = c;
left = 0;
right = 0;
}
Node () {
left = 0;
right = 0;
}
};


void preorder (int root, Node *node) {
if (root! = 0) {
cout << Node[root].val;
Preorder (node[root].left, node);
Preorder (node[root].right, node);
}
}
int main () {
int n;
while (CIN >> N) {
int Isroot[max];
Node Node[max];
memset (isroot, 0, sizeof (isroot));
int ID, left, right;
char value;
for (int i = 0; i < n; i++) {
CIN >> ID >> value >> left >> right;
Node[id].id = ID;
Node[id].val = value;
Node[id].left = left;
Node[id].right = right;
isroot[id]++;
isroot[left]++;
isroot[right]++;
}
int root;
for (int i = 0; i < MAX; i++) {
If (Isroot[i]% 2 = = 1) {//number of times the node is the root
root = i;
}
}
Preorder (root, node);
cout << Endl;
}
return 0;
}

1156. Binary tree root finding method with two forks

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.