https://leetcode.com/explore/interview/card/top-interview-questions-easy/94/trees/555/
/**
* 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
'Coding Test > LeetCode' 카테고리의 다른 글
[LeetCode] (Sorting and Searching) Lv Easy. First Bad Version (0) | 2023.01.22 |
---|---|
[LeetCode] (Sorting and Searching) Lv Easy. Merge Sorted Array (0) | 2023.01.22 |
[LeetCode] (Linked List) Lv Easy. Delete Node in a Linked List (0) | 2023.01.16 |
[LeetCode] (Strings) Lv Easy. Reverse Integer (0) | 2023.01.16 |
[LeetCode] (Array) Lv Easy. Best Time to Buy and Sell Stock II (0) | 2023.01.16 |
댓글