How to determine whether a binary tree is symmetrical in PHP
Jan 13, 2018 am 11:25 AMThis article mainly introduces the method of judging whether a binary tree is symmetrical in PHP, and involves the related operating skills of judging nodes in PHP recursive binary trees. Friends who need it can refer to it. I hope it can help everyone.
Question
Please implement a function to determine whether a binary tree is symmetrical. Note that a binary tree is defined as symmetric if it is the same as the image of the binary tree.
Solution
Recursively judge both sides of the binary tree.
Implementation code:
##
<?php /*class TreeNode{ var $val; var $left = NULL; var $right = NULL; function __construct($val){ $this->val = $val; } }*/ function isSymmetrical($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); }
JavaScript Implement pre-order, in-order and post-order traversal methods of binary trees
Detailed explanation of the definition method of complete binary trees in php
js Implement data structures: trees and binary trees, binary tree traversal and basic operation methods
The above is the detailed content of How to determine whether a binary tree is symmetrical in PHP. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

How To Set Up Visual Studio Code (VS Code) for PHP Development
