반응형
104. Maximum Depth of Binary Tree
LeetCode - The World's Leading Online Programming Learning Platform
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
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) {
if(!root){//루트가 없을경우에 0리턴
return 0
}
var leftTree = maxDepth(root.left)//왼쪽 재귀돌리고
var rightTree = maxDepth(root.right)///오른쪽 재귀돌리고
return Math.max(leftTree, rightTree)+1//그중에 큰값 리턴
};
이렇게짯는데 리턴문에 +1 을해줘야 된다 재귀돌때 +1씩해줘야하니까 ...
풀었는데 찝찝하다 항상
Math.max 같은 함수 알아두면 쉽게풀것같다 조건문으로 3줄나오던것을 한줄로
반응형
'코딩테스트' 카테고리의 다른 글
206. Reverse Linked List (1) | 2024.01.31 |
---|---|
108. Convert Sorted Array to Binary Search Tree (0) | 2024.01.24 |
226. Invert Binary Tree (0) | 2024.01.23 |
200. Number of Islands (0) | 2024.01.23 |
101. Symmetric Tree (0) | 2024.01.20 |