> For the complete documentation index, see [llms.txt](https://shangan.gitbook.io/algorithm/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://shangan.gitbook.io/algorithm/he-xin-suan-fa-200-ti/untitled-2/maximum-depth-of-binary-tree.md).

# Maximum Depth of Binary Tree

{% tabs %}
{% tab title="Java" %}

```java
// Solution 1
class Solution {
		public int maxDepth(TreeNode root) {
				if (root == null) return 0;
				int max_left = maxDepth(root.left);
				int max_right = maxDepth(root.right);
				return Math.max(max_left, max_right) + 1;
		}
}


// Solution 2
class Solution {
		public int maxDepth(TreeNode root) {
				return getHeight(root);
		}
		
		public int getHeight(TreeNode root) {
			if (root == null) return 0; //corner case
			
			int left = getHeight(root.left);
			int right = getHeight(root.right);
			
			return Math.max(left, right) + 1;
		}
		
		public int getMinHeight(TreeNode root) {
			if (root == null) return 0; //corner case
			
			int left = getHeight(root.left);
			int right = getHeight(root.right);
			
			return (left == 0 || right == 0) ? left + right + 1 : Math.min(left, right) + 1 ;
		}
}



```

{% endtab %}
{% endtabs %}
