본문 바로가기
Coding Test/LeetCode

[LeetCode] (Trees) Lv Easy. Maximum Depth of Binary Tree

by song.ift 2023. 1. 21.

https://leetcode.com/explore/interview/card/top-interview-questions-easy/94/trees/555/

 

Explore - LeetCode

LeetCode Explore is the best place for everyone to start practicing and learning on LeetCode. No matter if you are a beginner or a master, there are always new topics waiting for you to explore.

leetcode.com

 

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
var maxDepth = function(root) {
    let maxDepth = 0;
    
    const dfs = (root, curDepth) => {
        if (!root)
            return;
        
        maxDepth = Math.max(maxDepth, curDepth);
        
        dfs(root.left, curDepth + 1);
        dfs(root.right, curDepth + 1);
    };
    
    dfs(root, 1);
    
    return maxDepth;
};

GitHub : https://github.com/developeSHG/Algorithm-LeetCode/tree/main/maximum-depth-of-binary-tree

 

GitHub - developeSHG/Algorithm-LeetCode: 릿코드 소스코드

릿코드 소스코드. Contribute to developeSHG/Algorithm-LeetCode development by creating an account on GitHub.

github.com

 

댓글