1ms Java accepted solution


  • 2
    B
    public class Solution {
        public static int rob(TreeNode root) {
            Solution result = solve(root);
            return Math.max(result.with, result.without);
        }
        
        private static Solution solve(TreeNode root) {
            if (root == null) return new Solution();
    
            Solution left  = solve(root.left);
            Solution right = solve(root.right);
    
            Solution result = new Solution();
            result.with     = Math.max(root.val + left.without + right.without, left.with + right.with);
            result.without  = left.with + right.with;
            return result;
        }
    
        private int with = 0;
        private int without = 0;
    };

  • 0
    N
    This post is deleted!

Log in to reply
 

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