文章

34

粉丝

9

获赞

5

访问

8.8k

头像
二叉树的深度 题解:数组实现方法
P4356
发布于2024年6月29日 16:13
阅读数 207

#include <iostream>

using namespace std;

const int N = 1e5 + 10;

int pos,len,t,cnt = 0;

char tree[N]; // 存储树的数组 
char str[N]; // 存储输入字符串的数组 

void create(int pos){
	char c = str[t ++];
	if(c == '#'){
		return;
	}
	tree[pos] = c;
	create(pos * 2);
	create(pos * 2 + 1);
}


int get_depth(int root){
	if(tree[root] == 0){
		return 0;
	}
	int left = get_depth(2 * root);
	int right = get_depth(2 * root + 1);
	return max(left,right) + 1;
}

int main(){
	while(cin >> str){
		t = 0;
		create(1);
		cout << get_depth(1) << endl;
		
	}
	
	return 0;
}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发