A Google interview questions - reverse binary tree

Reverse binary tree

The general idea: storing the position of switching two child nodes directly input, if there is any node empty, a -1 is represented, after re-use order for the preamble of contribution before dfs traversal out, after the contribution using recursive preorder ,
using an auxiliary queue for the input sequence traversal requires no spaces so that the last vector using the details stored answer then processed output

Input processing part of the code

//初始化输入输出  
void init(){  
    cin >> n;  
    char a, b;  
    for (int i = 0; i < n; i++){  
        cin >> a >> b;  
        int c, d;  
        if (a == '-')c = -1;  
        else { c = a - '0'; vis[c] = 1; }  
        if (b == '-')d = -1;  
        else { d = b - '0'; vis[d] = 1;}  
        map[i].push_back(d);map[i].push_back(c);  
    }  
}  
  

Find the root sequence order of the first draw

Core code:

int findroot(){//找爹函数  
    for (int i = 0; i < n; i++){  
        if (vis[i] == false)return i;  
    }
void dfsTrave(int root){//获取前序遍历  
    if (root == -1){  
        BT.push_back(-1);  
        return;  
    }  
    else BT.push_back(root);  
    for (int i = 0; i < map[root].size(); i++)  
        dfsTrave(map[root][i]);  
    return;  
}      
  

Obtained in sequence preorder traversal and

void levelorder(Tree *root){//层序遍历  
    queue<Tree*> q;  
    q.push(root);  
    while (!q.empty()){  
        Tree *newnode = q.front();  
        q.pop();levelor.push_back(newnode->data);  
        if (newnode->left != nullptr)q.push(newnode->left);  
        if (newnode->right != nullptr)q.push(newnode->right);  
    }  
    return;  
}  

void inorer(Tree *root){//中序遍历  
    if (root == nullptr)return;  
    inorer(root->left);  
    inor.push_back(root->data);  
    inorer(root->right);  
}  

The complete code

#include <iostream>  
#include <vector>  
#include <queue>  
using namespace std;  
int n = 0,k = 0;  
//map存储二叉树的关系  
//由于输出需要 最后一个位置没有空格所以利用容器进行辅助控制输出  
//   BT : 存储dfs遍历得到的前序序列  
vector<int> map[100],inor,levelor,BT;  
bool vis[100]; //bool 数组用于辅助找到根节点  
typedef struct node{//链式二叉树结构  
    int data;  
    struct node *left,*right;  
}Tree;  
  
//初始化输入输出  
void init(){  
    cin >> n;  
    char a, b;  
    for (int i = 0; i < n; i++){  
        cin >> a >> b;  
        int c, d;  
        if (a == '-')c = -1;  
        else { c = a - '0'; vis[c] = 1; }  
        if (b == '-')d = -1;  
        else { d = b - '0'; vis[d] = 1;}  
        map[i].push_back(d);map[i].push_back(c);  
    }  
}  
  
int findroot(){//找爹函数  
    for (int i = 0; i < n; i++){  
        if (vis[i] == false)return i;  
    }  
}  
  
void dfsTrave(int root){//获取前序遍历  
    if (root == -1){  
        BT.push_back(-1);  
        return;  
    }  
    else BT.push_back(root);  
    for (int i = 0; i < map[root].size(); i++)  
        dfsTrave(map[root][i]);  
    return;  
}  
  
Tree *BuildTree(){//根据前序遍历建立数  
    Tree *node = new Tree;  
    if (BT[k] != -1){  
        node->data = BT[k];  
        k++;node->left = BuildTree();  
        k++;node->right = BuildTree();  
    }  
    else return nullptr;  
    return node;  
}  
void inorer(Tree *root){//中序遍历  
    if (root == nullptr)return;  
    inorer(root->left);  
    inor.push_back(root->data);  
    inorer(root->right);  
}  
void levelorder(Tree *root){//层序遍历  
    queue<Tree*> q;  
    q.push(root);  
    while (!q.empty()){  
        Tree *newnode = q.front();  
        q.pop();levelor.push_back(newnode->data);  
        if (newnode->left != nullptr)q.push(newnode->left);  
        if (newnode->right != nullptr)q.push(newnode->right);  
    }  
    return;  
}  
int main(){  
    init();  
    int root = findroot(); //找到根节点进行  
    dfsTrave(root);        //dfs获取前序并进行建树  
    Tree *start = BuildTree();  
    levelorder(start); //进行层序遍历  
    //输出答案部分  
    for (int i = 0; i < levelor.size() - 1; i++)  
        printf("%d%c", levelor[i], i != levelor.size() - 1 ? ' ':'\n');  
    inorer(start);    //进行中序遍历  
    for (int i = 0; i < inor.size() - 1; i++)  
        printf("%d%c", inor[i], i != inor.size() - 1 ? ' ' : '\n');  
    return 0;  
}  

Guess you like

Origin www.cnblogs.com/wlw-x/p/11681428.html