The offer-of the sword refers to whether the binary tree is symmetrical-java

Source: Internet
Author: User

Topic Description :

Please implement a function to determine whether a binary tree is symmetrical. Note that if a binary tree is the same as the mirror image of the binary tree, it is defined as symmetric.

thought Analysis : The judgment is not the empty knot point, yes, directly, it's symmetrical. Determine whether the left and right subtrees on either side are symmetric and the left and right subtrees are empty, return true, have one null and false, and determine whether two nodes are equal, and then the left subtree is equal to the right-hand subtree of the Saozi tree. Two equal in the middle.

Code :

public class TreeNode { 
    int val = 0; 
    TreeNode left = null; 
    TreeNode right = null; 
 
 
    Public TreeNode (int val) { 
        this.val = val; 
 
 
    } 
} 
* * Public  
class Solution {  
    boolean issymmetrical (TreeNode proot  
    )  
        {if (proot = null)/NULL is directly symmetric  
            return true;  
        Return issymmetrical (Proot.right,proot.left);  
    }  
    Boolean issymmetrical (TreeNode Right,treenode left) {//To determine whether or not to  
        be equal if (right==null && left==null)/left and right subtree are empty return  
            true;  
        if (Right==null | | left==null)//left and right subtree has an empty return  
            false;  
        The right and left nodes are equal and the points to the left of the right subtree are equal, and the middle part is equal to return  
        left.val==right.val&&issymmetrical (Right.right, Left.left) && issymmetrical (left.right,left.right);  
    }  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.