g0101_0200.s0104_maximum_depth_of_binary_tree.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java Show documentation
Show all versions of leetcode-in-java Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
package g0101_0200.s0104_maximum_depth_of_binary_tree;
// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Depth_First_Search #Breadth_First_Search
// #Tree #Binary_Tree #Data_Structure_I_Day_11_Tree
// #Programming_Skills_I_Day_10_Linked_List_and_Tree #Udemy_Tree_Stack_Queue
// #2022_06_22_Time_0_ms_(100.00%)_Space_42.9_MB_(67.03%)
import com_github_leetcode.TreeNode;
/*
* 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;
* }
* }
*/
public class Solution {
public int maxDepth(TreeNode root) {
return findDepth(root, 0);
}
private int findDepth(TreeNode node, int currentDepth) {
if (node == null) {
return 0;
}
currentDepth++;
return 1
+ Math.max(findDepth(node.left, currentDepth), findDepth(node.right, currentDepth));
}
}