当前位置:网站首页>Sword finger offer question brushing record 1

Sword finger offer question brushing record 1

2022-07-06 22:30:00 Mayor of Macondo

The finger of the sword offer Record of writing questions

101. Symmetric binary tree

️ Give you the root node of a binary tree root , Check whether it is axisymmetric

️ Example 1:

 Input :root = [1,2,2,3,4,4,3]
 Output :true

️ Example 2:

 Input :root = [1,2,2,null,3,null,3]
 Output :false

️ Tips :

  • The number of nodes in the tree is in the range [1, 1000] Inside

  • -100 <= Node.val <= 100

class Solution {
    
    public boolean isSymmetric(TreeNode root) {
    
  if(root==null){
    
      return true;
  }
  return isMirror(root,root);
    }
      boolean isMirror(TreeNode r1,TreeNode r2){
    
       if(r1==null&&r2==null){
    
           return true;
       }
       if(r1==null||r2==null){
    
           return false;
       }
       return (r1.val==r2.val)&&isMirror(r1.left,r2.right)&&isMirror(r1.right,r2.left);

  }
}
原网站

版权声明
本文为[Mayor of Macondo]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207061445138861.html