是否对称?

噢莫加纳加加加  •  1个月前


bool judge(int root1,int root2)
{
	if(root1==-1&&root2==-1)//左右都是-1,空树,返回true
	{
		return true;
	}
	if(root1==-1&&root2!=-1||root1!=-1&&root2==-1)//一个是空树,一个不是空树,返回false
	{
		return false;
	}
	cnt++;
	if(tree[root1].val!=tree[root2].val)//树的节点上的权值不相等,返回false
	{
		return false;
	}
	return judge(tree[root1].l,tree[root2].r)&&judge(tree[root1].r,tree[root2].l);//递归往下去求这两个树的子树是不是对称的
}


评论:

请先登录,才能进行评论