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 );}