-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path337.php
38 lines (34 loc) · 818 Bytes
/
337.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
<?php
/**
* Definition for a binary tree node.
* class TreeNode {
* public $val = null;
* public $left = null;
* public $right = null;
* function __construct($value) { $this->val = $value; }
* }
*/
class Solution
{
/**
* @param TreeNode $root
* @return Integer
*/
public function rob($root)
{
$res = $this->dfs($root);
return max($res);
}
public function dfs($node)
{
if ($node == null) {
return [0, 0];
}
$left = $this->dfs($node->left);
$right = $this->dfs($node->right);
$dp = [];
$dp[0] = max($left[0], $left[1]) + max($right[0], $right[1]); //当前跟节点不抢
$dp[1] = $node->val + $left[0] + $right[0]; //当前跟节点抢
return $dp;
}
}