LowestCommonAncestorOfABinarySearchTree   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 15
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
eloc 9
c 2
b 0
f 0
dl 0
loc 15
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A lowestCommonAncestor() 0 13 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace leetcode;
6
7
use leetcode\util\TreeNode;
8
9
class LowestCommonAncestorOfABinarySearchTree
10
{
11
    public static function lowestCommonAncestor(TreeNode $root, TreeNode $p, TreeNode $q): ?TreeNode
12
    {
13
        while ($root) {
14
            if ($p->val < $root->val && $q->val < $root->val) {
15
                $root = $root->left;
16
            } elseif ($p->val > $root->val && $q->val > $root->val) {
17
                $root = $root->right;
18
            } else {
19
                return $root;
20
            }
21
        }
22
23
        return null;
24
    }
25
}
26