您的位置:首页 > 其它

华为机试:给出一个字符串形式表达的二叉树,求出指定节点深度。

2016-03-26 15:07 686 查看
给出一个字符串形式表达的二叉树,求出指定节点深度。

输入的树形结构字符串格式为:

1、以父节点、左子树、右子树表示的二叉树;每个父节点不会超过两个子节点;

2、树的每一个节点采用单个字母表示;树的层次采用数字表示,树根的层次为1,下一层为2,不会超过9层;

3、字符串以“节点名称 层次数 节点名称 层次数…”的形式出现,同一个父节点下,先出现的为左子树。

例如字符串“a1b2c2d3e3f3”生成一棵如下的树:

         a

       /   \

      b     c

     / \   / 

    d   e f     

节点a的深度为3,节点b的深度是2,节点f的深度是1

输入:一行字符串,表示一个二叉树。一行字符串,一个字符一个节点,输入确保字符不会存在重复节点

输出:指定节点的深度,如果节点不存在,返回0;整数之间用空格隔开

例如:

输入:a1b2c2d3e3f3

ab

输出:3 2

[cpp] view
plain copy

#include<iostream>  

#include<string>  

using namespace std;  

  

int main()  

{     

    string str,user_str;  

    getline(cin,str);  

    getline(cin,user_str);  

    int len=str.size();  

    int user_len=user_str.size();  

       int maxDepth=atoi(&str[len-1]);//最大的深度;  

    for (int i=0;i<user_len;i++)  

    {  

        char user_tmp=user_str[i];  

        for(int j=0;j<len;j++)  

        {  

            if (user_tmp==str[j])  

            {  

                int position=atoi(&str[++j]);  

                cout<<maxDepth-position+1<<" ";  

                break;  

            }  

            if (j==len-1)  

            {  

                cout<<0<<" ";  

                break;  

            }  

        }  

    }  

    cout<<endl;  

    system("pause");  

}  

方案二:
#include <iostream>

#include <map>

#include <string>

#include <vector>

#include <queue>

using namespace std;

struct Node{
char data;
int level;
int l, r;
Node(char data, int level){
this->data = data;
this->level = level;
l = -1; // NULL
r = -1;
}

};

// static list

vector<Node> v; // maxsize is 1024

queue<int> q[10]; // 1~9 level, store the index of the v

map<char, int> m; // char2index

int findDeep(int ind, int deep){  // index, current deep
Node node = v[ind];
int lDeep = deep, rDeep = deep;

if(node.l != -1){
lDeep = findDeep(node.l, 1+deep);

if(node.r != -1){
rDeep = findDeep(node.r, 1+deep);
}
if(lDeep > rDeep){
return lDeep;
} else{
return rDeep;
}

}

int main(){
string str;
cin >> str;
for(int i = 0; i < str.length(); i += 2){
int level = str[i+1]-'0';
Node newNode = Node(str[i], level);
v.push_back(newNode);

int curIndex = v.size()-1;
m[str[i]] = curIndex;
q[level].push(curIndex);

// start to insert 
if(!q[level-1].empty()){
int fatherIndex = q[level-1].front();
if( v[fatherIndex].l == -1){ // the father node is empty
v[fatherIndex].l = curIndex;
} else{
v[fatherIndex].r = curIndex;
q[level-1].pop();             // the father node is full
}
}
}

// start to query
string query;
cin >> query;
for(int i = 0; i < query.length(); i++){
if(m.find(query[i]) != m.end()){
int ind = m[query[i]];

// cout << "index is " << ind << endl;

// if(v[ind].l != -1){

// cout << v[v[ind].l].data << endl;

// }if(v[ind].r != -1){

// cout << v[v[ind].r].data << endl;

// } cout << endl;

cout << findDeep(ind, 1); // current deep is 1
} else {
cout << 0;
}
if(i != query.length()-1){
cout << ' ';
} else{
cout << endl;
}
}
return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  华为 二叉树 结构