> For the complete documentation index, see [llms.txt](https://heunnajo.gitbook.io/algorithms-problem-solving-skills/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://heunnajo.gitbook.io/algorithms-problem-solving-skills/graph-dfs-bfs/maximum-depth-of-binary-tree.md).

# 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.

![](/files/-MPZgS2wPWP83u6AFSYU)

**자료구조** : N/A

**알고리즘**\
1\. 재귀호출을 통해 잎노드까지 내려간다. - Null을 만날 때까지 재귀호출로 쭉 내려간다!\
2\. Null을 만나면 비로소 값을 가지게 된다! 0 또는 1. - left:0, right:0, Math.max(0,0)+1 = 1

**알고리즘을 Java로**

```java
package graph;
import java.util.*;

class TreeNode {
	     int val;
	     TreeNode left;
	     TreeNode right;
	     TreeNode() {}
	     TreeNode(int val) { this.val = val; }
	     TreeNode(int val, TreeNode left, TreeNode right) {
	         this.val = val;
	         this.left = left;
	         this.right = right;
	     }
	 }
public class MaxDepthBT {//recursive call

	public static void main(String[] args) {
		TreeNode root = new TreeNode(3);
		root.left = new TreeNode(1);
		root.right = new TreeNode(4);
		root.left.left = new TreeNode(5);
		root.left.right = new TreeNode(8);
		root.left.left.left = new TreeNode(7);
		
		System.out.println(solve(root));
	}
	
	public static int solve(TreeNode root) {
		if(root == null) return 0;
		
		int left = solve(root.left);
		int right = solve(root.right);
		return Math.max(left, right)+1;
	}

}

```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://heunnajo.gitbook.io/algorithms-problem-solving-skills/graph-dfs-bfs/maximum-depth-of-binary-tree.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
