-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaximumDepthOfBinaryTree.java
More file actions
36 lines (34 loc) · 1020 Bytes
/
MaximumDepthOfBinaryTree.java
File metadata and controls
36 lines (34 loc) · 1020 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*
Given the root of a binary tree, return its maximum depth.
A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
Example 1:
Input: root = [3,9,20,null,null,15,7]
Output: 3
*/
public class MaximumDepthOfBinaryTree {
public static void main(String[] args) {
TreeNode root=new TreeNode(3);
root.left=new TreeNode(9);
root.right=new TreeNode(20);
root.left.left=null;
root.left.right=null;
root.right.left=new TreeNode(15);
root.right.right=new TreeNode(7);
int ans = maxDepth(root);
System.out.println(ans);
}
public static int maxDepth(TreeNode root) {
if (root==null){
return 0;
}
return 1+Math.max(maxDepth(root.left),maxDepth(root.right));
}
}
class TreeNode{
int data;
TreeNode left,right;
public TreeNode(int key){
key=data;
left=right=null;
}
}