Skip to content

Latest commit

 

History

History
26 lines (24 loc) · 454 Bytes

File metadata and controls

26 lines (24 loc) · 454 Bytes
  • 检查左右子树是否镜像即可
class Solution {
 public:
  bool isSymmetric(TreeNode* root) {
    if (!root) {
      return true;
    }
    return dfs(root->left, root->right);
  }

  bool dfs(TreeNode* l, TreeNode* r) {
    if (!l && !r) {
      return true;
    }
    if (!l || !r) {
      return false;
    }
    if (l->val != r->val) {
      return false;
    }
    return dfs(l->left, r->right) && dfs(l->right, r->left);
  }
};