TreeNode::dfsTreeValues()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 6
c 2
b 0
f 0
dl 0
loc 10
rs 9.6111
cc 5
nc 5
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace leetcode\util;
6
7
class TreeNode
8
{
9
    public $val = 0;
10
    public $left;
11
    public $right;
12
13
    public function __construct(int $val = 0, $left = null, $right = null)
14
    {
15
        $this->val = $val;
16
        $this->left = $left;
17
        $this->right = $right;
18
    }
19
20
    public static function dfsTreeValues(?TreeNode $tree, array &$list): void
21
    {
22
        if ($tree instanceof TreeNode) {
23
            $list[] = $tree->val ?: null;
24
25
            if ($tree->left) {
26
                self::dfsTreeValues($tree->left, $list);
27
            }
28
            if ($tree->right) {
29
                self::dfsTreeValues($tree->right, $list);
30
            }
31
        }
32
    }
33
34
    public static function bfsTreeValues(?TreeNode $tree): array
35
    {
36
        if (!$tree) {
37
            return [];
38
        }
39
        $ans = $queue = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $queue is dead and can be removed.
Loading history...
40
        $queue = [$tree];
41
        while ($queue) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $queue of type array<integer,leetcode\util\TreeNode> is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
42
            /** @var TreeNode $node */
43
            $node = array_shift($queue);
44
            array_push($ans, $node->val ?: null);
45
            if ($node->left) {
46
                array_push($queue, $node->left);
47
            }
48
49
            if ($node->right) {
50
                array_push($queue, $node->right);
51
            }
52
        }
53
54
        return $ans;
55
    }
56
57
    public static function fromArray(array $array): ?TreeNode
58
    {
59
        if (empty($array)) {
60
            return null;
61
        }
62
        [$queue, $root] = [[], new TreeNode($array[0])];
63
        [$i, $n] = [0, count($array)];
64
        $queue[] = $root;
65
66
        while ($i < $n) {
67
            $node = array_shift($queue);
68
            if ($node) {
69
                if (isset($array[++$i])) {
70
                    $value = $array[$i];
71
                    $node->left = new TreeNode($value);
72
                    $queue[] = $node->left;
73
                }
74
                if (isset($array[++$i])) {
75
                    $value = $array[$i];
76
                    $node->right = new TreeNode($value);
77
                    $queue[] = $node->right;
78
                }
79
            }
80
        }
81
82
        return $root;
83
    }
84
}
85