1102 Invert a Binary Tree

Posted Yohoc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1102 Invert a Binary Tree相关的知识,希望对你有一定的参考价值。

题目:

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 N1. Then N lines follow, each corresponds to a node from 0 to N1, 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

 

题目大意:根据输入构建二叉树,求出一个倒转二叉树(左右子树交换)的层序和中序遍历结果。

注意: inverted binary tree: 倒转二叉树(左右子树交换)

 

代码:(中序遍历 + 层序遍历)

#include<stdio.h>
#include<iostream>
#include<queue>
using namespace std;
int n, root;
bool isChild[12], f = 0;
struct Node
    int lchild, rchild;
node[12];
void levelorder(int x)
    bool flag = false;
    queue<int> q;
    q.push(x);
    while(!q.empty())
        int tmp = q.front();
        q.pop();
        if(!flag)
            flag = true;
        else
         cout<<" ";   
        
        cout<<tmp;
        if(node[tmp].rchild != -1)
            q.push(node[tmp].rchild);
         
        if(node[tmp].lchild != -1)
            q.push(node[tmp].lchild);
        
    

void inorder(int x)
    if(node[x].rchild != -1)
        inorder(node[x].rchild);
    
    if(!f)
        f = 1;
    else
        cout<<" ";
    
    cout<<x;
    if(node[x].lchild != -1)
        inorder(node[x].lchild);
    

int main()
    scanf("%d", &n);
    
    for(int i = 0; i < n; i++)
        char l, r;
        cin>>l>>r;
        // scanf("%c%c", &l, &r);
        if(l != \'-\')
            node[i].lchild = l - \'0\';
            isChild[l - \'0\'] = 1;
        else
            node[i].lchild = -1;
        
        if(r != \'-\')
            node[i].rchild = r - \'0\';
            isChild[r - \'0\'] = 1;
        else
            node[i].rchild = -1;
        
    
   
    for(int i = 0 ; i < n; i++)
        if(isChild[i]==0)
            root = i;
            break;
        
    
    levelorder(root);
    cout<<endl;
    inorder(root);
    return 0;

 

Invert Binary Tree

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* invertTree(TreeNode* root) {
        if(!root)
            return root;
        if(root->left||root->right)
        {
            TreeNode* temp=root->left;
            root->left=root->right;
            root->right=temp;
            if(root->left)
                invertTree(root->left);
            if(root->right)
                invertTree(root->right);
        }
        
        return root;
        
    }
};

 

以上是关于1102 Invert a Binary Tree的主要内容,如果未能解决你的问题,请参考以下文章

1102 Invert a Binary Tree

1102. Invert a Binary Tree (25)

PAT1102: Invert a Binary Tree

PAT-1102(Invert a Binary Tree)

1102. Invert a Binary Tree (25)

PAT Advanced 1102 Invert a Binary Tree (25分)