Infiltrate a Binary Tree
Question
0
/ \
2 3
/
4Solution
public int infiltrateATree(TreeNode root) {
if(root==null) return 0;
return Math.max(infiltrateATree(root.left),infiltrateATree(root.right))+root.val;
}Follow up 1:
public int infiltrateAManyTree(ManyTreeNode root) {
if(root==null) return 0;
int max = 0;
for(ManyTreeNode mnode:root.childList) {
max = Math.max(max,infiltrateAKTree(mnode));
}
return max+root.val;
}Follow up 2:
Last updated