leetcode.com/problems/maximum-depth-of-binary-tree/
Given the root of a binary tree, return its maximum depth.
A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
Input: root = [3,9,20,null,null,15,7]
Output: 3
node가 null일때까지 깊이를 확인해보면 된다.
class Solution {
public int maxDepth(TreeNode root) {
return checkDepth(root, 0);
}
public int checkDepth(TreeNode node, int depth) {
if (node == null) {
return depth;
}
return Math.max(checkDepth(node.left, depth + 1), checkDepth(node.right, depth + 1));
}
}
'개발자 > algorithm' 카테고리의 다른 글
LeetCode, Move Zeroes (Java) (0) | 2021.01.05 |
---|---|
LeetCode, Invert Binary Tree (Java) (0) | 2021.01.03 |
LeetCode, Merge Two Binary Trees (Java) (0) | 2021.01.03 |
LeetCode, Custom Sort String (Java) (0) | 2020.12.30 |
LeetCode, Check If Two String Arrays are Equivalent (Java) (0) | 2020.12.30 |