All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0101_0200.s0104_maximum_depth_of_binary_tree.Solution.kt Maven / Gradle / Ivy

There is a newer version: 1.30
Show newest version
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_09_02_Time_236_ms_(83.39%)_Space_36.2_MB_(88.26%)

import com_github_leetcode.TreeNode
import kotlin.math.max

/*
 * Example:
 * var ti = TreeNode(5)
 * var v = ti.`val`
 * Definition for a binary tree node.
 * class TreeNode(var `val`: Int) {
 *     var left: TreeNode? = null
 *     var right: TreeNode? = null
 * }
 */
class Solution {
    fun maxDepth(root: TreeNode?): Int {
        return findDepth(root, 0)
    }

    private fun findDepth(node: TreeNode?, currentDepth: Int): Int {
        var localCurrentDepth = currentDepth
        if (node == null) {
            return 0
        }
        localCurrentDepth++
        return 1 + max(findDepth(node.left, localCurrentDepth), findDepth(node.right, localCurrentDepth))
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy