Leetcode 101. Symmetric Tree

Posted zhangwj0101

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 101. Symmetric Tree相关的知识,希望对你有一定的参考价值。

Question

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

Code

  public boolean judge(TreeNode left, TreeNode right) 
        if (left == null && right == null) 
            return true;
         else if (left == null && right != null || left != null && right == null) 
            return false;
        
        return left.val == right.val && judge(left.left, right.right) && judge(left.right, right.left);

    


    public boolean isSymmetric(TreeNode root) 
        if (root == null) 
            return true;
        

        return judge(root.left, root.right);
    

以上是关于Leetcode 101. Symmetric Tree的主要内容,如果未能解决你的问题,请参考以下文章

[LeetCode]题解(python):101 Symmetric tree

[LeetCode]题解(python):101-Symmetric Tree

LeetCode OJ 101Symmetric Tree

LeetCode OJ 101Symmetric Tree

Leetcode 101. Symmetric Tree

LeetCode 101:Symmetric Tree