解题思路:题解思路值得借鉴。因为题目要求最小变动的秤砣数量,所以至少有一个秤砣是不变的。我们可以固定某一个子秤砣,根据其深度(确定为dfs参数之一)得到整个天平的重量,若其他子秤砣固定求得的天平重量与其相等,则表示固定某一个秤砣,其余这些无需变动。因此我们可以求得各个天平重量出现的次数,即定义一个map与其映射。比较得到最大的出现次数,并用子结点个数减去该次数即为最小变动次数。这题的dfs设计并不容易,注意好好理解
题目描述:给一个深度不超过16的二叉树,代表一个天平。每根杆都悬挂在中间,每个秤砣的重量已知。至少修改多少个秤砣的质量才能让天平平衡?
Sample Input
3 [[3,7],6] 40 [[2,3],[4,5]]
Sample Output
1 0 3
#include<cstdio>
#include<iostream>
#include<sstream>
#include<string>
#include<cstring>
#include<cctype>
#include<map>
using namespace std;
string s;
map<long long,int>Q;
int sum;
void DFS(int depth,int s1,int e1) //深度,遍历起点与终点
{
if(s[s1]=='[')
{
int p=0;
for(int i=s1+1;i!=e1;i++)
{
if(s[i]=='[') p++;
if(s[i]=&#