PAT 1102 Invert a Binary Tree

1102 Invert a Binary Tree(25分)

The following is from Max Howell @twitter:

Google: 90% of our engineers use the software you wrote (Homebrew), but you can't invert a binary tree on a whiteboard so fuck off.

Now it's your turn to prove that YOU CAN invert a binary tree!

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integerN(≤10) which is the total number of nodes in the tree -- and hence the nodes are numbered from 0 toN−1. ThenNlines follow, each corresponds to a node from 0 toN−1, and gives the indices of the left and right children of the node. If the child does not exist, a-will be put at the position. Any pair of children are separated by a space.

Output Specification:

For each test case, print in the first line the level-order, and then in the second line the in-order traversal sequences of the inverted tree. There must be exactly one space between any adjacent numbers, and no extra space at the end of the line.

Sample Input:

8
1 -
- -
0 -
2 7
- -
- -
5 -
4 6

Sample Output:

3 7 2 6 4 0 5 1
6 5 7 4 3 2 0 1

思路

  • 本题的意思是反转二叉树,并输出反转之后的树的层次遍历和中序遍历。
  • 投机取巧:无需反转二叉树,在层次遍历和中序遍历时将原本的左右子树的先后顺序颠倒过来。
  • 严格按照定义操作:在后序遍历中,交换左右子树的顺序,所得即为反转后的二叉树。
  • 对于一些重复的操作,可以使用函数来定义,以使代码更加简洁。

代码

  • 第一次:
#include 
#include 

using namespace std;

const int maxn = 20;

struct node
{
    int data;
    int lchild, rchild;
}Node[maxn]; 
int n;
bool noRoot[maxn];
char c1, c2;

// 反转的后序遍历 
int num = 0;
void postOrder(int root) 
{
    if (root == -1)
        return;
    postOrder(Node[root].rchild);
    printf("%d", Node[root].data);
    if (num < n - 1)
        printf(" ");
    num ++;
    
    postOrder(Node[root].lchild);
}
// 反转后的层次遍历
int num1 = 0;
void  layerOrder(int root)
{
    if (root == -1)
        return;
    queue q;
    q.push(root);
    while (!q.empty())
    {
        int now = q.front();
        q.pop();
        printf("%d", Node[now].data);
        if (num1 < n - 1)
        {
            printf(" ");
        }
        num1 ++;
        if (Node[now].rchild != -1)
            q.push(Node[now].rchild);
        if (Node[now].lchild != -1)
            q.push(Node[now].lchild);
    }
}


int main()
{
    //freopen("test.txt", "r", stdin);
    
    scanf("%d", &n);
    for (int i = 0; i < n; i ++)
    {
        getchar();
        scanf("%c %c", &c1, &c2);
        Node[i].data =  i;
        if (c1 != '-')
        {
            Node[i].lchild = c1 - '0';
            noRoot[c1 - '0'] = true;
        }
        else 
        {
            Node[i].lchild = -1;
        }
        
        if (c2 != '-')
        {
            Node[i].rchild = c2 - '0';
            noRoot[c2 - '0'] = true;
        }
        else
        {
            Node[i].rchild = -1;
        }
    }
    
    int k;
    for (int i = 0; i < n; i ++)
        if (!noRoot[i])
            k = i;

    layerOrder(k);
    printf("\n");
    postOrder(k);
    
    return 0;
}
  • 改进版:
#include 
#include 
#include 

using namespace std;

const int maxn = 110;

struct node
{
    int lchild, rchild;
}Node[maxn];
bool noRoot[maxn];

int n, num = 0;

void print(int id)
{
    printf("%d", id);
    num ++;
    if (num < n)
        printf(" ");
    else
        printf("\n");
}

// 中序遍历 
void inOrder(int root) 
{
    if (root == -1)
        return;
    inOrder(Node[root].lchild);
    print(root);
    inOrder(Node[root].rchild);
}

// 层次遍历
void layerOrder(int root) 
{
    queue q;
    q.push(root);
    while (!q.empty())
    {
        int now = q.front();
        q.pop();
        print(now);
        if (Node[now].lchild != -1)    q.push(Node[now].lchild);
        if (Node[now].rchild != -1) q.push(Node[now].rchild);
    }
}

// 后序遍历, 反转二叉树

void postOrder(int root) 
{
    if (root == -1)
        return;
    postOrder(Node[root].lchild);
    postOrder(Node[root].rchild);
    swap(Node[root].lchild, Node[root].rchild);    //交换左右孩子 
}

int strToNum(char c) 
{
    if (c == '-')
        return -1;
    else
    {
        noRoot[c - '0'] = true;
        return c - '0';
    }
}

int findRoot()
{
    for (int i = 0; i < n; i ++)
    {
        if (noRoot[i] == false)
            return i;
    }
}

int main()
{
    char lchild, rchild;
    scanf("%d", &n);
    for (int i = 0; i < n; i ++)
    {
        getchar();
        scanf("%c %c", &lchild, &rchild);
        Node[i].lchild = strToNum(lchild);
        Node[i].rchild = strToNum(rchild);
    }
    int root = findRoot();
    postOrder(root);
    layerOrder(root);
    num = 0;
    inOrder(root);
    
    return 0;
}

你可能感兴趣的:(算法c++)