๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿƒ algorithm/leetcode

LeetCode 104 - Maximum Depth of Binary Tree (Easy)

by HandHand 2021. 3. 3.

๋ฌธ์ œ

LeetCode - 104๋ฒˆ

ํ’€์ด ๊ณผ์ •

์ด์ง„ ํŠธ๋ฆฌ์˜ ๋†’์ด ๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

๊ฐ„๋‹จํ•˜๊ฒŒ 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

๐Ÿ’ฌ ๋Œ“๊ธ€