-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathbalance_binary_tree.php
53 lines (46 loc) · 1.11 KB
/
balance_binary_tree.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
<?php
/**
* 剑指 Offer:判断一棵树是否是平衡二叉树
* Author:学院君
*/
include_once 'binary_tree_node.php';
/**
* @param BinaryTreeNode $root 二叉树根节点
* @param int $depth 二叉树结点深度
* @return bool
*/
function isBalanced($root, &$depth): bool
{
if ($root == null) {
$depth = 0;
return true;
}
$left = $right = 0;
if (isBalanced($root->left, $left) && isBalanced($root->right, $right)) {
$diff = abs($left - $right);
if ($diff <= 1) {
$depth = 1 + ($left > $right ? $left : $right);
return true;
}
}
return false;
}
// 测试代码
$node1 = new BinaryTreeNode();
$node1->data = 1;
$node2 = new BinaryTreeNode();
$node2->data = 2;
$node3 = new BinaryTreeNode();
$node3->data = 3;
$node4 = new BinaryTreeNode();
$node4->data = 4;
$node1->left = $node2;
$node1->right = $node3;
$node3->right = $node4;
$depth = 0;
var_dump(isBalanced($node1, $depth)); // true
$node5 = new BinaryTreeNode();
$node5->data = 5;
$node4->left = $node5;
$depth = 0;
var_dump(isBalanced($node1, $depth)); // false