leetcode.com/problems/maximum-depth-of-binary-tree/

 

Maximum Depth of Binary Tree - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

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));
	}
}

+ Recent posts