1 solutions

  • 1
    @ 2026-11-18 18:59:41

    C++ :

    #include <bits/stdc++.h>
    #define N 100005
    using namespace std;
    struct Node{
    	int l,r,deep;
    	bool full;
    } tre[N];
    
    int ans;
    
    void dfs(int root,int deep){
    	if (!root) return;
    	
    	Node nod = tre[root];
    	if (nod.l == 0 && nod.r == 0 ) {
    		ans ++;
    		tre[root].deep = deep;
    		tre[root].full  = 1;	
    	}
    	else{
    		dfs(nod.l,deep + 1);
    		dfs(nod.r,deep + 1);
    		Node nod_l = tre[nod.l],nod_r = tre[nod.r];
    		tre[root].deep = max(nod_r.deep,nod_l.deep);
    		if (nod_l.deep == nod_r.deep && nod_l.full && nod_r.full){
    			ans ++;
    			tre[root].full = 1;
    		}
    		else{
    			tre[root].full = 0;
    		}
    	} 
    }
    
    int main(){
    	int n;
    	cin >> n;
    	for (int i = 1;i<=n;i++){
    		cin >> tre[i].l >> tre[i].r;
    	}
    	
    	dfs(1,1);
    	cout << ans;
    	return 0;
    }
    
    • 1

    Information

    ID
    350
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    10
    Tags
    # Submissions
    7
    Accepted
    3
    Uploaded By