Logo

dev-resources.site

for different kinds of informations.

House robber III

Published at
10/5/2024
Categories
dp
leetcod
tree
binary
Author
prashantrmishra
Categories
4 categories in total
dp
open
leetcod
open
tree
open
binary
open
Author
15 person written this
prashantrmishra
open
House robber III

Problem

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) ;
    }
}

Enter fullscreen mode Exit fullscreen mode
tree Article's
30 articles in total
Favicon
House robber III
Favicon
Inorder traversal of a binary tree
Favicon
Comprehensive Tree Care Solutions in Gig Harbor and Tacoma with Pablo Tree Services
Favicon
Tree data structures in Rust with tree-ds (#2: Tree Operations)
Favicon
The Benefits of Hiring Professional Tree Trimmers in Christchurch
Favicon
Tree data structures in Rust with tree-ds (#1: Getting Started)
Favicon
DFS Traversal Guide: Easy way to remember DFS Traversel Path
Favicon
Chain - a Goofy, Functional, Tree-backed List
Favicon
Demystifying Tree Lopping vs. Tree Chipping: Which is Right for Your Landscape?
Favicon
Ergonomic Trees in Go
Favicon
Generating Dynamic Breadcrumb Menus Using Tree Table & Recursive CTE
Favicon
Types of decision tree in machine learning
Favicon
Tree Service Tips: Keeping Your Property Safe in Tinley Park
Favicon
What is tree data structure? 🌳
Favicon
Golang multinode tree with parallel search
Favicon
Implementing Nested Filters using React and Tree Data Structure
Favicon
814. Binary Tree Pruning
Favicon
A hierarchical tree component for React in Typescript
Favicon
C++ - Basic Tree Traversal(Recursive vs Queue)
Favicon
C++ - Basic Tree Traversal(Recursive vs Stack)
Favicon
Generate files and folder structures of your code
Favicon
Converting materialized paths into a tree with generics: a Golang kata
Favicon
wishing3 - if you'd 3 wishes, what'd they be?
Favicon
Represent a directory tree in a Github README.md
Favicon
Segment Trees - Part I
Favicon
A Nibble of Quadtrees in Rust
Favicon
What is AST?
Favicon
Finding all children of a node in a tree
Favicon
5 Reasons You Need Tree Doctor to Keep Your Plants Healthy
Favicon
Binary Tree Pruning

Featured ones: