๋ฌธ์
ํ์ด ๊ณผ์
์ด์ง ํธ๋ฆฌ์ ๋์ด
๋ฅผ ๊ตฌํ๋ ๋ฌธ์ ์
๋๋ค.
๊ฐ๋จํ๊ฒ DFS
๋ฅผ ์ํํด์ ์ผ์ชฝ ์๋ธํธ๋ฆฌ์ ์ค๋ฅธ์ชฝ ์๋ธํธ๋ฆฌ ์ค์์ ๋์ height
๋ฅผ ์ ํํ๋๋ก ํ์ต๋๋ค.
์ฝ๋
/**
* 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) {
function dfs(root) {
if (!root) return 0;
const maxHeight = Math.max(dfs(root.left) + 1, dfs(root.right) + 1);
return maxHeight;
}
const answer = dfs(root);
return answer;
};
๋ฐ์ํ
'๐ algorithm > leetcode' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
LeetCode 206 - Reverse Linked List (Easy) (0) | 2021.03.03 |
---|---|
LeetCode 136 - Single Number (Easy) (0) | 2021.03.03 |
LeetCode 102 - Binary Tree Level Order Traversal (Medium) (0) | 2021.03.03 |
LeetCode 617 - Merge Two Binary Trees (Easy) (0) | 2021.03.03 |
LeetCode 98 - Validate Binary Search Tree (Medium) (0) | 2021.03.03 |
๐ฌ ๋๊ธ