PHP implements a method to determine whether a binary tree is symmetric. php judges a binary tree.

Source: Internet
Author: User

PHP implements a method to determine whether a binary tree is symmetric. php judges a binary tree.

This example describes how PHP can determine whether a binary tree is symmetric. We will share this with you for your reference. The details are as follows:

Problem

Implement a function to determine whether a binary tree is symmetric. Note: If a binary tree is the same as the image of this binary tree, it is defined as symmetric.

Question

Recursively determine the sides of a binary tree.

Implementation Code:

<? Php/* class TreeNode {var $ val; var $ left = NULL; var $ right = NULL; function _ construct ($ val) {$ this-> val = $ val ;}} */function isequalrical ($ pRoot) {if ($ pRoot = null) return true; return compare ($ pRoot-> left, $ pRoot-> right);} function compare ($ root1, $ root2) {if ($ root1 = null & $ root2 = null) return true; if ($ root1 = null | $ root2 = null) return false; if ($ root1-> val! = $ Root2-> val) return false; return compare ($ root1-> left, $ root2-> right) & compare ($ root1-> right, $ root2-> left );}

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.