Skip to content

Commit

Permalink
Create MaximumDepthOfTree.java
Browse files Browse the repository at this point in the history
  • Loading branch information
harshithasudhakar authored Dec 19, 2024
1 parent e4fdd89 commit 3e0daa6
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions Binary Tree/MaximumDepthOfTree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/

import java.util.*;
class MaximumDepthOfTree {
public int maxDepth(TreeNode root) {
return postorder(root);
}

public static int postorder(TreeNode root){
if(root == null){
return 0;
}

int leftDepth = postorder(root.left);
int rightDepth = postorder(root.right);
return 1+Math.max(leftDepth, rightDepth);
}
}

0 comments on commit 3e0daa6

Please sign in to comment.