101. 对称二叉树https://leetcode.cn/problems/symmetric-tree/
给你一个二叉树的根节点
root
, 检查它是否轴对称。示例 1:
输入:root = [1,2,2,3,4,4,3] 输出:true
- class Solution {
- public boolean isSymmetric(TreeNode root) {
- return check(root,root);
- }
-
- public boolean check(TreeNode p, TreeNode q){
- if(p==null&&q==null){
- return true;
- }
- if(p==null||q==null){
- return false;
- }
- return p.val==q.val && check(p.left,q.right)&&check(p.right,q.left);
- }
- }