From ac7645023cf4d23c8f99295eb44ac4b91941ad17 Mon Sep 17 00:00:00 2001 From: limlim Date: Mon, 17 Feb 2025 23:43:38 +0900 Subject: [PATCH] maximum depth of binary tree solution --- maximum-depth-of-binary-tree/limlimjo.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 maximum-depth-of-binary-tree/limlimjo.js diff --git a/maximum-depth-of-binary-tree/limlimjo.js b/maximum-depth-of-binary-tree/limlimjo.js new file mode 100644 index 000000000..d98f33e45 --- /dev/null +++ b/maximum-depth-of-binary-tree/limlimjo.js @@ -0,0 +1,23 @@ +// 시간 복잡도: O(n) +// 공간 복잡도: O(n) + +/** + * 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) return 0; + + let leftDepth = maxDepth(root.left); + let rightDepth = maxDepth(root.right); + + return Math.max(leftDepth, rightDepth) + 1; +};