Skip to content

Commit 1ab3975

Browse files
authored
Create 104. Maximum Depth of Binary Tree
1 parent 226e384 commit 1ab3975

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

104. Maximum Depth of Binary Tree

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/***
2+
Given a binary tree, find its maximum depth.
3+
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
4+
***/
5+
6+
//Java solution:
7+
8+
/**
9+
* Definition for a binary tree node.
10+
* public class TreeNode {
11+
* int val;
12+
* TreeNode left;
13+
* TreeNode right;
14+
* TreeNode(int x) { val = x; }
15+
* }
16+
*/
17+
public class Solution {
18+
public int maxDepth(TreeNode root) {
19+
if(root == null){
20+
return 0;
21+
}
22+
23+
int left = maxDepth(root.left);
24+
int right = maxDepth(root.right);
25+
int result = Math.max(left, right) + 1; // plus root
26+
27+
return result;
28+
}
29+
}

0 commit comments

Comments
 (0)