Simple Solution with Diagrams in Video - JavaScript, C++, Java, Python
Video\nPlease upvote here so others save time too!\n\nLike the video on YouTube if you found it useful\n\nClick here to subscribe on YouTube:\nhttps://www.youtu
# Video\nPlease upvote here so others save time too!\n\nLike the video on YouTube if you found it useful\n\nClick here to subscribe on YouTube:\nhttps://www.youtube.com/@mayowadan?sub_confirmation=1\n\nThanks!\n\nhttps://youtu.be/Yf9kCchl7No?si=Okmp9Wir2xZyjP3J\n\n```Javascript []\n/**\n * Definition for a binary tree node.\n * function TreeNode(val, left, right) {\n *     this.val = (val===undefined ? 0 : val)\n *     this.left = (left===undefined ? null : left)\n *     this.right = (right===undefined ? null : right)\n * }\n */\n/**\n * @param {TreeNode} root\n * @return {number}\n */\nvar rob = function (root) {\n    // Returns maximum value from the pair: [includeRoot,\n    // excludeRoot]\n    const result = heist(root);\n    return Math.max(result[0], result[1]);\n};\n\nfunction heist(root) {\n    // Empty tree case\n    if (root === null) {\n        return [0, 0];\n    }\n    // Recursively calculating the maximum amount that can\n    // be robbed from the left subtree of the root\n    const leftSubtree = heist(root.left);\n    // Recursively calculating the maximum amount that can\n    // be robbed from the right subtree of the root\n    const rightSubtree = heist(root.right);\n    // includeRoot contains the maximum amount of money\n    // that can be robbed with the parent node included\n    const includeRoot =\n        root.val + leftSubtree[1] + rightSubtree[1];\n    // excludeRoot contains the maximum amount of money\n    // that can be robbed with the parent node excluded\n    const excludeRoot =\n        Math.max(leftSubtree[0], leftSubtree[1]) +\n        Math.max(rightSubtree[0], rightSubtree[1]);\n\n    return [includeRoot, excludeRoot];\n}\n```\n```Python []\n# Definition for a binary tree node.\n# class TreeNode:\n#     def __init__(self, val=0, left=None, right=None):\n#         self.val = val\n#         self.left = left\n#         self.right = right\n\n\nclass Solution:\n    def rob(self, root: Optional[TreeNode]) -> int:\n        # Returns maximum value from the pair: [includeRoot, excludeRoot]\n        result = self.heist(root)\n        return max(result[0], result[1])\n\n    def heist(self, root: Optional[TreeNode]) -> List[int]:\n        # Empty tree case\n        if root is None:\n            return [0, 0]\n\n        # Recursively calculating the maximum amount that can be robbed\n        # from the left subtree of the root\n        leftSubtree = self.heist(root.left)\n\n        # Recursively calculating the maximum amount that can be \n        # robbed from the right subtree of the root\n        rightSubtree = self.heist(root.right)\n\n        # includeRoot contains the maximum amount of money that can be\n        # robbed with the parent node included\n        includeRoot = root.val + leftSubtree[1] + rightSubtree[1]\n\n        # excludeRoot contains the maximum amount of money that can be\n        # robbed with the parent node excluded\n        excludeRoot = max(leftSubtree[0], leftSubtree[1]) + max(\n            rightSubtree[0], rightSubtree[1]\n        )\n\n        return [includeRoot, excludeRoot]\n```\n```Java []\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n * int val;\n * TreeNode left;\n * TreeNode right;\n * TreeNode() {}\n * TreeNode(int val) { this.val = val; }\n * TreeNode(int val, TreeNode left, TreeNode right) {\n * this.val = val;\n * this.left = left;\n * this.right = right;\n * }\n * }\n */\n\nclass Solution {\n    public int rob(TreeNode root) {\n        // Returns maximum value from the pair: [includeRoot, excludeRoot]\n        int[] result = heist(root);\n        return Math.max(result[0], result[1]);\n    }\n\n    private int[] heist(TreeNode root) {\n        // Empty tree case\n        if (root == null) {\n            return new int[] { 0, 0 };\n        }\n\n        // Recursively calculating the maximum amount that can be robbed from the left\n        // subtree of the root\n        int[] leftSubtree = heist(root.left);\n\n        // Recursively calculating the maximum amount that can be robbed from the right\n        // subtree of the root\n        int[] rightSubtree = heist(root.right);\n\n        // includeRoot contains the maximum amount of money that can be robbed with the\n        // parent node included\n        int includeRoot = root.val + leftSubtree[1] + rightSubtree[1];\n\n        // excludeRoot contains the maximum amount of money that can be robbed with the\n        // parent node excluded\n        int excludeRoot = Math.max(leftSubtree[0], leftSubtree[1]) + Math.max(rightSubtree[0], rightSubtree[1]);\n\n        return new int[] { includeRoot, excludeRoot };\n    }\n}\n```\n```C++ []\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n *     int val;\n *     TreeNode *left;\n *     TreeNode *right;\n *     TreeNode() : val(0), left(nullptr), right(nullptr) {}\n *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),\n * right(right) {}\n * };\n */\nclass Solution {\npublic:\n    int rob(TreeNode* root) {\n        // Returns maximum value from the pair: {includeRoot, excludeRoot}\n        vector<int> result = heist(root);\n        return max(result[0], result[1]);\n    }\n\nprivate:\n    vector<int> heist(TreeNode* root) {\n        // Empty tree case\n        if (root == nullptr) {\n            return {0, 0};\n        }\n\n        // Recursively calculating the maximum amount that can be robbed from\n        // the left subtree of the root\n        vector<int> leftSubtree = heist(root->left);\n\n        // Recursively calculating the maximum amount that can be robbed from\n        // the right subtree of the root\n        vector<int> rightSubtree = heist(root->right);\n\n        // includeRoot contains the maximum amount of money that can be robbed\n        // with the parent node included\n        int includeRoot = root->val + leftSubtree[1] + rightSubtree[1];\n\n        // excludeRoot contains the maximum amount of money that can be robbed\n        // with the parent node excluded\n        int excludeRoot = max(leftSubtree[0], leftSubtree[1]) +\n                          max(rightSubtree[0], rightSubtree[1]);\n\n        return {includeRoot, excludeRoot};\n    }\n};\n```
['Python', 'C++', 'Java', 'Python3', 'JavaScript']