华为机试:给出一个字符串形式表达的二叉树,求出指定节点深度。
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;
}
输入的树形结构字符串格式为:
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;
}
相关文章推荐
- AVL树-自平衡二叉查找树(Java实现)
- 注册表的组织结构
- 华为路由器密码恢复
- 华为交换机的后缀详解
- SQLSERVER的非聚集索引结构深度理解
- 调整SQLServer2000运行中数据库结构
- C#基础语法:结构和类区别详解
- 深入c# 类和结构的区别总结详解
- c#结构和类的相关介绍
- C#中结构(struct)的部分初始化和完全初始化实例分析
- C#中类与结构的区别实例分析
- C#枚举类型与结构类型实例解析
- javascript实现表现、结构、行为分离的选项卡效果!
- C语言二叉树的非递归遍历实例分析
- 使用C语言构建基本的二叉树数据结构
- 一波二叉树遍历问题的C++解答实例分享
- C++非递归队列实现二叉树的广度优先遍历
- C#使用前序遍历、中序遍历和后序遍历打印二叉树的方法
- 实用的js 焦点图切换效果 结构行为相分离
- C#非递归先序遍历二叉树实例