例题6-9UVA839 - Not so Mobile 递归访问二叉树

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=780

思路:递归访问二叉树

Code:

#include 
#include 
using namespace std;

bool solve(int &w){
	int w1,d1,w2,d2;
	bool b1 = true , b2 = true;
	cin >> w1 >> d1 >> w2 >> d2 ;
	if( !w1 ){ b1 = solve(w1); }
	if( !w2 ){ b2 = solve(w2); }
	w = w1 + w2;
	return b1 && b2 && ( w1 * d1 == w2 * d2 );
}
int main(){
	int T;
	int w ;
	cin >> T;
	while( T-- ){
		if( solve(w) ){
			cout << "YES" << endl;
		}else {
			cout << "NO" << endl;
		}
		if(T) cout << endl;
	}
	return 0;
}

你可能感兴趣的:(算法竞赛入门经典-数据结构基础,数据结构-树)