1102 Invert a Binary Tree (25 分)dfs+层序+中序+后序遍历

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 integer N (≤10) which is the total number of nodes in the tree -- and hence the nodes are numbered from 0 to N?1. Then N lines follow, each corresponds to a node from 0 to N?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

思路:  这个题目的难点是寻找树根,我的办法是使用dfs,从哪一个结点出发能够访问到所有的结点,那么该结点就是树根。可以认为存储的是一个有n-1条边的有向图,只有从根节点出发才能到达所有的结点。然后使用后序遍历进行镜像反转,按层序和后序输出即可。
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<string>
#include<map>
#include<set>
using namespace std;

vector<int> visited;

struct Node
{

    int lchild,rchild;
};
Node node[11];
void findRoot(int root)
{
    if(root==-1)
        return;
    visited.push_back(root);
    findRoot(node[root].lchild);
    findRoot(node[root].rchild);
}

void invert(int root)
{
    if(root!=-1)
    {
        invert(node[root].lchild);
        invert(node[root].rchild);
        swap(node[root].lchild,node[root].rchild);
    }
}

void levelOrder(int root)
{
    queue<int>qu;
    qu.push(root);
    cout<<root;
    while(!qu.empty())
    {
        int temp=qu.front();
        qu.pop();
        if(node[temp].lchild!=-1)
        {
            qu.push(node[temp].lchild);
            cout<<" "<<node[temp].lchild;
        }
        if(node[temp].rchild!=-1)
        {
            qu.push(node[temp].rchild);
            cout<<" "<<node[temp].rchild;
        }
    }
}
vector<int> in_order;
void inOrder(int root)
{
    if(root!=-1)
    {
        inOrder(node[root].lchild);
        in_order.push_back(root);
        inOrder(node[root].rchild);
    }
}

int main()
{
    int n;
    cin>>n;

    for(int i=0;i<n;i++)
    {
        string temp1,temp2;
        cin>>temp1>>temp2;
        if(temp1[0]==‘-‘)
            node[i].lchild=-1;
        else
            node[i].lchild=atoi(temp1.c_str());
        if(temp2[0]==‘-‘)
            node[i].rchild=-1;
        else
            node[i].rchild=atoi(temp2.c_str());
    }
    int i;
    for(i=0;i<n;i++)
    {
        findRoot(i);
        if(visited.size()==n)
            break;
        else
            visited.clear();
    }
    //cout<<i<<endl;
    invert(i);
    levelOrder(i);
    inOrder(i);
    cout<<endl;
    cout<<in_order[0];
    for(int i=1;i<in_order.size();i++)
        cout<<" "<<in_order[i];
    return 0;
}


原文地址:https://www.cnblogs.com/zhanghaijie/p/10306358.html

时间: 2024-08-08 07:10:47

1102 Invert a Binary Tree (25 分)dfs+层序+中序+后序遍历的相关文章

PAT Advanced 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 Specif

1102. Invert a Binary Tree (25)【二叉树】——PAT (Advanced Level) Practise

题目信息 1102. Invert a Binary Tree (25) 时间限制400 ms 内存限制65536 kB 代码长度限制16000 B 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.

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 Specif

PAT (Advanced Level) 1102. Invert a Binary Tree (25)

简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; struct Node { int left; int right; }s[20]; int

1110 Complete Binary Tree (25分) 判断一棵二插树是否是完全二叉树

Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each input file contains one test case. For each case, the first line gives a positive integer N (≤) which is the total number of nodes in the tree -- and he

【PAT甲级】1110 Complete Binary Tree (25分)

题意: 输入一个正整数N(<=20),代表结点个数(0~N-1),接着输入N行每行包括每个结点的左右子结点,'-'表示无该子结点,输出是否是一颗完全二叉树,是的话输出最后一个子结点否则输出根节点. trick: 用char输入子结点没有考虑两位数的结点??... stoi(x)可以将x转化为十进制整数 AAAAAccepted code: 1 #define HAVE_STRUCT_TIMESPEC 2 #include<bits/stdc++.h> 3 using namespace

二叉树遍历 A1102. Invert a Binary Tree (25) 反转二叉树 并输出层序和中序遍历 (使用静态二叉树)

#include <bits/stdc++.h> #include <stdio.h> #include <stdlib.h> #include <queue> using namespace std; const int maxn = 110; struct node{ int lchild,rchild; }Node[maxn]; bool notRoot[maxn] = {false};//记录是否不是根节点,初始均是根节点 int n,num = 0

PAT-1102(Invert a Binary Tree)

题目见这里   和1099略微相似,考察二叉树和基本的遍历,算是简单的啦,下标还充当了数据域,主要是知道要标记访问到的下标,从而确定root //1102:Invert a Binary Tree #include <cstdio> #include <iostream> using namespace std; const int N = 10; typedef struct node{ int lChild,rChild; }Node; void Input(int &

1043 Is It a Binary Search Tree (25 分)

1043 Is It a Binary Search Tree (25 分) A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a no