House Robber III

The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the "root." Besides the root, each house has one and only one parent house. After a tour, the smart thief realized that "all houses in this place forms a binary tree". It will automatically contact the police if two directly-linked houses were broken into on the same night.
Determine the maximum amount of money the thief can rob tonight without alerting the police.

Example 1:

Input: [3,2,3,null,3,null,1]

     3
    / \
   2   3
    \   \ 
     3   1

Output: 7 

Approach

Java

public class HouseRobberIII {
    public static void main(String[] args) {
        TreeNode root = new TreeNode(3);
        root.left = new TreeNode(2);
        root.right = new TreeNode(3);
        root.left.right = new TreeNode(3);
        root.right.right = new TreeNode(1);
        System.out.println(rob(root));
    }

    static int dfs(TreeNode root) {
        if (root == null)
            return 0;
        int sum = 0;
        // call for left subtree
        if (root.left != null)
            sum += dfs(root.left);

        // call for right subtree
        if (root.right != null)
            sum += dfs(root.right);
        int temp = 0;
        if (root.left != null) {
            if (root.left.left != null)
                temp += root.left.left.val;
            if (root.left.right != null)
                temp += root.left.right.val;
        }
        if (root.right != null) {
            if (root.right.left != null)
                temp += root.right.left.val;
            if (root.right.right != null)
                temp += root.right.right.val;
        }
        root.val = Math.max(root.val + temp, sum);
        return root.val;
    }

    static int rob(TreeNode root) {
        return dfs(root);
    }
}

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode() {
    }

    TreeNode(int val) {
        this.val = val;
    }

    TreeNode(int valTreeNode leftTreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

C++

#include <bits/stdc++.h>
using namespace std;
//struct for treenode
struct TreeNode{
    int data;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int data)
    {
        this->data=data;
        this->left=NULL;
        this->right=NULL;
    }
};


int dfs(TreeNode *root)
{
        if(root==NULL)
              return 0;
        int sum=0;
        //call for left subtree
        if(root->left)
              sum+=dfs(root->left);

        //call for right subtree
        if(root->right)
              sum+=dfs(root->right);
        int temp=0;
        if(root->left)
           {
               if(root->left->left)
                     temp+=root->left->left->data;
            if(root->left->right)
                 temp+=root->left->right->data;
           }
        if(root->right)
        {
            if(root->right->left)
                   temp+=root->right->left->data;
            if(root->right->right)
                   temp+=root->right->right->data;
        }
           root->data=max(root->data+temp,sum);
           return root->data;
    }
int rob(TreeNode* root
{
        return dfs(root);
}

int main()
{
    TreeNode *root=new TreeNode(3);
    root->left=new TreeNode(2);
    root->right=new TreeNode(3);
    root->left->right=new TreeNode(3);
    root->right->right=new TreeNode(1);
    cout<<rob(root);
}


No comments:

Post a Comment