Skip to content

Commit df63ab7

Browse files
committed
Chapter 07 section 01 C++ code updated. Java codes added.
1 parent 0f853cf commit df63ab7

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

2 files changed

+29
-2
lines changed

07-Binary-Tree-and-Recursion/Course Code (C++)/01-Maximum-Depth-of-Binary-Tree/main.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
using namespace std;
44

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+
510
/// Definition for a binary tree node.
611
struct TreeNode {
712
int val;
@@ -14,10 +19,10 @@ class Solution {
1419
public:
1520
int maxDepth(TreeNode* root) {
1621

17-
if( root == NULL )
22+
if(root == NULL)
1823
return 0;
1924

20-
return 1 + max( maxDepth(root->left) , maxDepth(root->right) );
25+
return 1 + max(maxDepth(root->left), maxDepth(root->right));
2126
}
2227
};
2328

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
}

0 commit comments

Comments
 (0)