반응형
1. 문제
https://leetcode.com/problems/maximum-depth-of-binary-tree/description/
2. 코드
/**
* 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) {
if(root === null) return 0;
return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
};
반응형
'리트코드 > easy' 카테고리의 다른 글
[리트코드] 136. Single Number - js (0) | 2023.07.19 |
---|---|
[리트코드] 118. Pascal's Triangle - js (0) | 2023.07.18 |
[리트코드] 206. Reverse Linked List - js (0) | 2023.07.12 |
[리트코드] 94. Binary Tree Inorder Traversal - js (0) | 2023.07.10 |
[리트코드] 226. Invert Binary Tree - js (0) | 2023.07.09 |