[algorithm][leetcode] 104. Maximum Depth of Binary Tree

임택·2020년 1월 18일
0

알고리즘

목록 보기
2/63
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    
  public int maxDepth(TreeNode root) {        
    return getMaxDepth(root, 0);
  }
    
  private int getMaxDepth(TreeNode root, int depth) {
      if (root == null) 
          return depth;
      depth++;
        
      int leftDepth = getMaxDepth(root.left, depth);
      int rightDepth = getMaxDepth(root.right, depth);
      int max = Math.max(leftDepth, rightDepth);
      return max;
  }
  
}
profile
캬-!

0개의 댓글