mirror of
https://github.com/TheAlgorithms/PHP.git
synced 2025-07-10 03:26:19 +02:00
code styling and refactoring for algorithms: Bellman-Ford, Dijkstras, ReverseLinkedList, CompareBinaryTree and InvertBinaryTree
This commit is contained in:
committed by
Brandon Johnson
parent
7edf1247c3
commit
d588cd1455
35
DataStructures/CompareBinaryTree/CompareBinaryTree.php
Normal file
35
DataStructures/CompareBinaryTree/CompareBinaryTree.php
Normal file
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace DataStructures\CompareBinaryTree;
|
||||
|
||||
/**
|
||||
* Recurrent comparison of binary trees based on comparison of left and right branches
|
||||
* (https://en.wikipedia.org/wiki/Binary_tree).
|
||||
*
|
||||
* @author Michał Żarnecki https://github.com/rzarno
|
||||
*/
|
||||
class CompareBinaryTree
|
||||
{
|
||||
/**
|
||||
* compare two binary trees
|
||||
* @param BinaryTreeNode|null $a
|
||||
* @param BinaryTreeNode|null $b
|
||||
* @return bool
|
||||
*/
|
||||
public function areTreesEqual(?BinaryTreeNode $a, ?BinaryTreeNode $b): bool
|
||||
{
|
||||
if (! $a && $b || $a && ! $b) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (! $a && ! $b) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($a->value !== $b->value) {
|
||||
return false;
|
||||
}
|
||||
return $this->areTreesEqual($a->left, $b->left)
|
||||
&& $this->areTreesEqual($a->right, $b->right);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user