dev-resources.site
for different kinds of informations.
House robber III
Published at
10/5/2024
Categories
dp
leetcod
tree
binary
Author
prashantrmishra
Author
15 person written this
prashantrmishra
open
TC: O(n) where n is the no. of nodes in the tree
SC: O(n) for using dp map and O(n) for recursive stack space
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
/*
cases for the any give node
//consider
if root.val is considered then we can explore root.left.left , root.left.right , root.right.left and root.right.right
//not consider
if root.val is not considered the we can explore root.left and root.right
return Integer.max(consider, not consider)
*/
public int rob(TreeNode root) {
Map<TreeNode,Integer> dp = new HashMap<>();
return max(root,dp);
}
public int max(TreeNode root,Map<TreeNode,Integer> dp){
if(root ==null )return 0;
if(dp.containsKey(root)) return dp.get(root);
int considerRoot = root.val;
if(root.left!=null){
considerRoot+=max(root.left.left,dp) + max(root.left.right,dp);
}
if(root.right!=null){
considerRoot+=max(root.right.left,dp) + max(root.right.right,dp);
}
int dontConsiderRoot = max(root.left,dp) + max(root.right,dp);
dp.put(root,Integer.max(considerRoot,dontConsiderRoot));
return dp.get(root) ;
}
}
tree Article's
30 articles in total
House robber III
currently reading
Inorder traversal of a binary tree
read article
Comprehensive Tree Care Solutions in Gig Harbor and Tacoma with Pablo Tree Services
read article
Tree data structures in Rust with tree-ds (#2: Tree Operations)
read article
The Benefits of Hiring Professional Tree Trimmers in Christchurch
read article
Tree data structures in Rust with tree-ds (#1: Getting Started)
read article
DFS Traversal Guide: Easy way to remember DFS Traversel Path
read article
Chain - a Goofy, Functional, Tree-backed List
read article
Demystifying Tree Lopping vs. Tree Chipping: Which is Right for Your Landscape?
read article
Ergonomic Trees in Go
read article
Generating Dynamic Breadcrumb Menus Using Tree Table & Recursive CTE
read article
Types of decision tree in machine learning
read article
Tree Service Tips: Keeping Your Property Safe in Tinley Park
read article
What is tree data structure? 🌳
read article
Golang multinode tree with parallel search
read article
Implementing Nested Filters using React and Tree Data Structure
read article
814. Binary Tree Pruning
read article
A hierarchical tree component for React in Typescript
read article
C++ - Basic Tree Traversal(Recursive vs Queue)
read article
C++ - Basic Tree Traversal(Recursive vs Stack)
read article
Generate files and folder structures of your code
read article
Converting materialized paths into a tree with generics: a Golang kata
read article
wishing3 - if you'd 3 wishes, what'd they be?
read article
Represent a directory tree in a Github README.md
read article
Segment Trees - Part I
read article
A Nibble of Quadtrees in Rust
read article
What is AST?
read article
Finding all children of a node in a tree
read article
5 Reasons You Need Tree Doctor to Keep Your Plants Healthy
read article
Binary Tree Pruning
read article
Featured ones: