- 问题
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree is symmetric:
1 / \ 2 2 / \ / \ 3 4 4 3
But the following is not:
1 / \ 2 2 \ \ 3 3
- 解答
bool isSymtotree(struct TreeNode *root1,struct TreeNode *root2) { if(root1==NULL && root2==NULL) return true; if(root1==NULL && root2!=NULL) return false; if(root1!=NULL && root2==NULL) return false; if(root1->val==root2->val) { return isSymtotree(root1->left,root2->right)&&isSymtotree(root1->right,root2->left); } return false; } bool isSymmetric(struct TreeNode* root) { if(root==NULL) return true; //struct TreeNode *root1=invertTree(root); //return isSameTree(root,root1); return isSymtotree(root->left,root->right); }
Symmetric Tree
最新推荐文章于 2022-04-21 23:25:34 发布