Much EZier to understand Java recursive


  • 0
    S
        public int rob(TreeNode root) {
            return Math.max(rob(root, true), rob(root, false));
        }
        
        public int rob(TreeNode root, boolean canRob) {
    		if (root == null)	return 0;
    		if (canRob)
    			return Math.max(rob(root.left, true) + rob(root.right, true),
    			    root.val + rob(root.left, false) + rob(root.right, false));
    		else
    			return rob(root.left, true) + rob(root.right, true);
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.