文章

37

粉丝

1

获赞

353

访问

24.7k

头像
关键节点的个数 题解:
P994 复旦大学2021年机试题
发布于 2026年3月
阅读数 613

#include<bits/stdc++.h>
using namespace std;
int cnt;
typedef struct node{
	int data;
	node* left;
	node* right;
}*BiTree;

BiTree build(vector<string> v){
	BiTree root = new node{stoi(v[0]),nullptr,nullptr};
	int idx = 1;
	queue<node*> q;
	q.push(root);
	
	while(idx < v.size() && !q.empty()){
		BiTree curr = q.front();
		q.pop();
		
		if(idx < v.size() && v[idx] != "null"){
			curr->left = new node{stoi(v[idx]),nullptr,nullptr};
			q.push(curr->left);
		}
		idx++;
		if(idx < v.size() && v[idx] != "null"){
			curr->right = new node{stoi(v[idx]),nullptr,nullptr};
			q.push(curr->right);
		}
		idx++;
	}
	return root;
}


void dfs(BiTree root,int max_val){
	if(root == nullptr){
		return ;
	}
	if(root->data >= max_val){
		cnt ++;
		max_val = root->data;
	}
	dfs(root->left,max_val);
	dfs(root->right,max_val);
}

int main(){
	string t;
	cin>>t;
	stringstre...
登录查看完整内容


登录后发布评论

暂无评论,来抢沙发