Saturday, February 23, 2019

LeetCode 104. Maximum Depth of Binary Tree




Example:
Given binary tree [3,9,20,null,null,15,7],
    3
   / \
  9  20
    /  \
   15   7
return its depth = 3.











class Solution {
public int maxDepth(TreeNode root) {
// corner case
if (root == null) {
return 0;
}
// call the childrens recursively
return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
}
}




No comments:

Post a Comment