-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCousinsInBinaryTree.php
66 lines (57 loc) · 1.88 KB
/
CousinsInBinaryTree.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
declare(strict_types=1);
namespace leetcode;
use leetcode\util\TreeNode;
class CousinsInBinaryTree
{
private int $xDepth = 0;
private int $yDepth = 0;
private ?TreeNode $xParent = null;
private ?TreeNode $yParent = null;
public static function isCousins(?TreeNode $root, int $x, int $y): bool
{
if (!$root) {
return false;
}
$queue = [$root];
while ($queue) {
[$curr, $n] = [[], count($queue)];
for ($i = 0; $i < $n; $i++) {
/** @var \leetcode\util\TreeNode $node */
$node = array_shift($queue);
if ($node->left) {
array_push($queue, $node->left);
$curr[$node->left->val] = $node->val;
}
if ($node->right) {
array_push($queue, $node->right);
$curr[$node->right->val] = $node->val;
}
}
$keys = array_keys($curr);
if (in_array($x, $keys) && in_array($y, $keys) && $curr[$x] !== $curr[$y]) {
return true;
}
}
return false;
}
public function isCousins2(?TreeNode $root, int $x, int $y): bool
{
$this->helper($root, $x, $y, 0, null);
return $this->xDepth === $this->yDepth && $this->xParent !== $this->yParent;
}
public function helper(?TreeNode $node, int $x, int $y, int $depth, ?TreeNode $parent)
{
if (!$node) {
return;
}
if ($node->val === $x) {
[$this->xDepth, $this->xParent] = [$depth, $parent];
} elseif ($node->val === $y) {
[$this->yDepth, $this->yParent] = [$depth, $parent];
} else {
$this->helper($node->left, $x, $y, $depth + 1, $node);
$this->helper($node->right, $x, $y, $depth + 1, $node);
}
}
}