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

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

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

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
  1. #include<iostream>  
  2. #include<string>  
  3. using namespace std;  
  4.   
  5. int main()  
  6. {     
  7.     string str,user_str;  
  8.     getline(cin,str);  
  9.     getline(cin,user_str);  
  10.     int len=str.size();  
  11.     int user_len=user_str.size();  
  12.        int maxDepth=atoi(&str[len-1]);//最大的深度;  
  13.     for (int i=0;i<user_len;i++)  
  14.     {  
  15.         char user_tmp=user_str[i];  
  16.         for(int j=0;j<len;j++)  
  17.         {  
  18.             if (user_tmp==str[j])  
  19.             {  
  20.                 int position=atoi(&str[++j]);  
  21.                 cout<<maxDepth-position+1<<" ";  
  22.                 break;  
  23.             }  
  24.             if (j==len-1)  
  25.             {  
  26.                 cout<<0<<" ";  
  27.                 break;  
  28.             }  
  29.         }  
  30.     }  
  31.     cout<<endl;  
  32.     system("pause");  
  33. }  

方案二:
#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;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值