File tree Expand file tree Collapse file tree 4 files changed +150
-129
lines changed
out/production/leetcode/com/blankj/easy/_104 Expand file tree Collapse file tree 4 files changed +150
-129
lines changed Original file line number Diff line number Diff line change 1+ # [ Maximum Depth of Binary Tree] [ title ]
2+
3+ ## Description
4+
5+ Given a binary tree, find its maximum depth.
6+
7+ The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
8+
9+ ** Tags:** Tree, Depth-first Search
10+
11+
12+ ## 思路
13+
14+ 题意是找到二叉树的最大深度,很明显,深搜即可,每深入一次节点加一即可,然后取左右子树的最大深度。
15+
16+ ``` java
17+ /**
18+ * Definition for a binary tree node.
19+ * public class TreeNode {
20+ * int val;
21+ * TreeNode left;
22+ * TreeNode right;
23+ * TreeNode(int x) { val = x; }
24+ * }
25+ */
26+ class Solution {
27+ public int maxDepth (TreeNode root ) {
28+ if (root == null ) return 0 ;
29+ return 1 + Math . max(maxDepth(root. left), maxDepth(root. right));
30+ }
31+ }
32+ ```
33+
34+
35+ ## 结语
36+
37+ 如果你同我一样热爱数据结构、算法、LeetCode,可以关注我GitHub上的LeetCode题解:[ awesome-java-leetcode] [ ajl ]
38+
39+
40+
41+ [ title ] : https://leetcode.com/problems/maximum-depth-of-binary-tree
42+ [ ajl ] : https://github.com/Blankj/awesome-java-leetcode
You can’t perform that action at this time.
0 commit comments