Ejemplo n.º 1
0
        public int Rob(TreeNode root)
        {
            if (root == null)
            {
                return(0);
            }
            if (root.left == null && root.right == null)
            {
                return(root.val);
            }
            DPTreeNode dproot = new DPTreeNode();

            DFS(root, dproot);
            return(Math.Max(dproot.With, dproot.Without));
        }
Ejemplo n.º 2
0
 public void DFS(TreeNode root, DPTreeNode dproot)
 {
     if (root.left != null)
     {
         DPTreeNode dpleft = new DPTreeNode();
         dproot.left = dpleft;
         DFS(root.left, dpleft);
     }
     if (root.right != null)
     {
         DPTreeNode dpright = new DPTreeNode();
         dproot.right = dpright;
         DFS(root.right, dpright);
     }
     dproot.With = ((dproot.left == null) ? 0 : dproot.left.Without) +
                   ((dproot.right == null) ? 0 : dproot.right.Without) + root.val;
     dproot.Without = ((dproot.left == null) ? 0 : Math.Max(dproot.left.Without, dproot.left.With)) +
                      ((dproot.right == null) ? 0 : Math.Max(dproot.right.Without, dproot.right.With));
 }