File tree Expand file tree Collapse file tree 2 files changed +29
-2
lines changed
07-Binary-Tree-and-Recursion
Course Code (C++)/01-Maximum-Depth-of-Binary-Tree
Course Code (Java)/01-Maximum-Depth-of-Binary-Tree/src Expand file tree Collapse file tree 2 files changed +29
-2
lines changed Original file line number Diff line number Diff line change 2
2
3
3
using namespace std ;
4
4
5
+ // 104. Maximum Depth of Binary Tree
6
+ // https://leetcode.com/problems/maximum-depth-of-binary-tree/description/
7
+ // 时间复杂度: O(n), n是树中的节点个数
8
+ // 空间复杂度: O(h), h是树的高度
9
+
5
10
// / Definition for a binary tree node.
6
11
struct TreeNode {
7
12
int val;
@@ -14,10 +19,10 @@ class Solution {
14
19
public:
15
20
int maxDepth (TreeNode* root) {
16
21
17
- if ( root == NULL )
22
+ if (root == NULL )
18
23
return 0 ;
19
24
20
- return 1 + max ( maxDepth (root->left ) , maxDepth (root->right ) );
25
+ return 1 + max (maxDepth (root->left ), maxDepth (root->right ));
21
26
}
22
27
};
23
28
Original file line number Diff line number Diff line change
1
+ // 104. Maximum Depth of Binary Tree
2
+ // https://leetcode.com/problems/maximum-depth-of-binary-tree/description/
3
+ // 时间复杂度: O(n), n是树中的节点个数
4
+ // 空间复杂度: O(h), h是树的高度
5
+ class Solution {
6
+
7
+ // Definition for a binary tree node.
8
+ public class TreeNode {
9
+ int val ;
10
+ TreeNode left ;
11
+ TreeNode right ;
12
+ TreeNode (int x ) { val = x ; }
13
+ }
14
+
15
+ public int maxDepth (TreeNode root ) {
16
+
17
+ if (root == null )
18
+ return 0 ;
19
+
20
+ return 1 + Math .max (maxDepth (root .left ), maxDepth (root .right ));
21
+ }
22
+ }
You can’t perform that action at this time.
0 commit comments