101. Symmetric Tree

Source: Internet
Author: User

    /** 101. Symmetric Tree * 2016-5-16 by Mingyang * Only one root does not work at first, the following is the standard English answer time complexity and spatial complexity of the expression * Because we Trave     RSE the entire input tree once, the total run time was O (n) o (n), * where NN is the total number of nodes in the tree.      * The number of recursive calls is bound by the height of the tree.      * In the worst case, the tree is linear and the height are in O (n) o (n).     * Therefore, space complexity due to recursive calls on the stack are O (n) o (n) in the worst case. * Of course we can do it with a good queue .*/     Public Booleanissymmetric (TreeNode root) {if(Root = =NULL)            return true; returnIssymmetrictree (Root.left, root.right); }     Public BooleanIssymmetrictree (TreeNode p, TreeNode q) {if(p = =NULL&& Q = =NULL)            return true; if(p = =NULL|| Q = =NULL)            return false; return(P.val = = q.val) && issymmetrictree (P.left, Q.right) &&Issymmetrictree (P.right, q.left); }

101. Symmetric Tree

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.