LeetCode 101. 对称二叉树
给定一个二叉树,检查它是否是镜像对称的。
例如,二叉树 [1,2,2,3,4,4,3]
是对称的。
但是下面这个 [1,2,2,null,3,null,3]
则不是镜像对称的:
算法:我们只需递归判断左右子树是否是镜像的即可。
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: bool dfs(TreeNode* p, TreeNode* q){ if(!p||!q)return !p&&!q; return p->val==q->val&&dfs(p->left,q->right)&&dfs(p->right,q->left); } bool isSymmetric(TreeNode* root) { if(!root)return true; return dfs(root->left,root->right); } };