2019-11-02 12:07:41 +08:00
|
|
|
|
# 55.1 二叉树的深度
|
|
|
|
|
|
2022-01-07 09:00:01 +00:00
|
|
|
|
[NowCoder](https://www.nowcoder.com/practice/435fb86331474282a3499955f0a41e8b?tpId=13\&tqId=11191\&tPage=1\&rp=1\&ru=/ta/coding-interviews\&qru=/ta/coding-interviews/question-ranking\&from=cyc\_github)
|
2019-11-02 12:07:41 +08:00
|
|
|
|
|
|
|
|
|
## 题目描述
|
|
|
|
|
|
|
|
|
|
从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。
|
|
|
|
|
|
2022-01-07 09:00:01 +00:00
|
|
|
|
\
|
|
|
|
|
|
2019-11-02 12:07:41 +08:00
|
|
|
|
|
|
|
|
|
## 解题思路
|
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
public int TreeDepth(TreeNode root) {
|
|
|
|
|
return root == null ? 0 : 1 + Math.max(TreeDepth(root.left), TreeDepth(root.right));
|
|
|
|
|
}
|
|
|
|
|
```
|