Advertisement
Guest User

Grokking #219 - 1

a guest
May 18th, 2022
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.53 KB | None | 0 0
  1.     int traverse(TreeNode root) {
  2.         int val = 0;
  3.        
  4.         if(root == null)
  5.             return 0;
  6.        
  7.         if (root.left != null) {
  8.             val += traverse(root.left.left) + traverse(root.left.right);
  9.         }
  10.         if (root.right != null) {
  11.             val += traverse(root.right.left) + traverse(root.right.right);
  12.         }
  13.  
  14.         return Math.max(val + root.val, traverse(root.left) + traverse(root.right));    
  15.     }
  16.    
  17.     public int rob(TreeNode root) {
  18.         return traverse(root);
  19.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement