判别两棵树是否相等。
判别两棵树是否相等。
正确答案:int compareTree(TreeNode* tree1, TreeNode* tree2) //用分治的方法做,比较当前根,然后比较左子树和右子树 {bool tree1IsNull = (tree1==NULL); bool tree2IsNull = (tree2==NULL); if(tree1IsNull != tree2IsNull) { return 1; } if(tree1IsNull &;&; tree2IsNull) {//如果两个都是NULL,则相等 return 0; }//如果根节点不相等,直接返回不相等,否则的话,看看他们孩子相等不相等 if(tree1->c != tree2->c) { return 1; } return (compareTree(tree1->left,tree2->left)&;compareTree(tree1->right,tree2->right)) (compareTree(tree1->left,tree2->right)&;compareTree(tree1->right,tree2->left)); }//算法结束