Symmetric Tree
判断一棵二叉树是否是镜面对称的,可以有递归和循环两种方法
以下是递归的方法,增加一个函数判断两棵树是对称的
bool isSymmetric2(TreeNode *tree1, TreeNode *tree2){ if(tree1 == NULL && tree2 == NULL) return true; if(tree1 == NULL || tree2 == NULL) return false; if(tree1->val != tree2->val) return false; return isSymmetric2(tree1->left, tree2->right)&&isSymmetric2(tree1->right, tree2->left); } bool isSymmetric(TreeNode *root) { // Start typing your C/C++ solution below // DO NOT write int main() function if(root == NULL) return true; else return isSymmetric2(root->left, root->right); }
循环的方法以后补充