Skip to content

Commit f819adb

Browse files
authored
Create 0337-house-robber-iii.js
1 parent cff997a commit f819adb

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

javascript/0337-house-robber-iii.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Post Order Traversal
3+
* Time O(n) | Space (n)
4+
* Definition for a binary tree node.
5+
* function TreeNode(val, left, right) {
6+
* this.val = (val===undefined ? 0 : val)
7+
* this.left = (left===undefined ? null : left)
8+
* this.right = (right===undefined ? null : right)
9+
* }
10+
*/
11+
/**
12+
* @param {TreeNode} root
13+
* @return {number}
14+
*/
15+
var rob = function(root) {
16+
17+
function dfs(root) {
18+
if(!root) return [0, 0];
19+
20+
const leftSubTree = dfs(root.left);
21+
const rightSubTree = dfs(root.right);
22+
23+
const withoutRoot = Math.max(...leftSubTree) + Math.max(...rightSubTree);
24+
const withRoot = root.val + leftSubTree[0] + rightSubTree[0];
25+
26+
return [withoutRoot, withRoot];
27+
}
28+
29+
return Math.max(...dfs(root));
30+
};

0 commit comments

Comments
 (0)