From 8c9ca9b3c78be1d20ce19681fa7b3127f447cf62 Mon Sep 17 00:00:00 2001 From: Yu Nakamura Date: Mon, 13 Apr 2026 10:29:29 +0900 Subject: [PATCH] add files --- 0104-maximum-depth-of-binary-tree/memo.md | 11 +++++++++++ 0104-maximum-depth-of-binary-tree/step1.cpp | 13 +++++++++++++ 0104-maximum-depth-of-binary-tree/step2.cpp | 13 +++++++++++++ 0104-maximum-depth-of-binary-tree/step3.cpp | 13 +++++++++++++ 4 files changed, 50 insertions(+) create mode 100644 0104-maximum-depth-of-binary-tree/memo.md create mode 100644 0104-maximum-depth-of-binary-tree/step1.cpp create mode 100644 0104-maximum-depth-of-binary-tree/step2.cpp create mode 100644 0104-maximum-depth-of-binary-tree/step3.cpp diff --git a/0104-maximum-depth-of-binary-tree/memo.md b/0104-maximum-depth-of-binary-tree/memo.md new file mode 100644 index 0000000..5dc1f44 --- /dev/null +++ b/0104-maximum-depth-of-binary-tree/memo.md @@ -0,0 +1,11 @@ +### step1 + +わからなかったのでChatGPTに解いてもらった。DFSで左右のnodeのうち深い方を取って+1していくことを繰り返すだけだった。 + +### step2 + +変更点なし。 + +### step3 + +3回通すまで書き直し。 diff --git a/0104-maximum-depth-of-binary-tree/step1.cpp b/0104-maximum-depth-of-binary-tree/step1.cpp new file mode 100644 index 0000000..824f313 --- /dev/null +++ b/0104-maximum-depth-of-binary-tree/step1.cpp @@ -0,0 +1,13 @@ +class Solution { +public: + int maxDepth(TreeNode* root) { + if (root == nullptr) { + return 0; + } + + int leftDepth = maxDepth(root->leftNode); + int rightDepth = maxDepth(root->rightNode); + + return max(leftDepth, rightDepth) + 1; + } +}; diff --git a/0104-maximum-depth-of-binary-tree/step2.cpp b/0104-maximum-depth-of-binary-tree/step2.cpp new file mode 100644 index 0000000..824f313 --- /dev/null +++ b/0104-maximum-depth-of-binary-tree/step2.cpp @@ -0,0 +1,13 @@ +class Solution { +public: + int maxDepth(TreeNode* root) { + if (root == nullptr) { + return 0; + } + + int leftDepth = maxDepth(root->leftNode); + int rightDepth = maxDepth(root->rightNode); + + return max(leftDepth, rightDepth) + 1; + } +}; diff --git a/0104-maximum-depth-of-binary-tree/step3.cpp b/0104-maximum-depth-of-binary-tree/step3.cpp new file mode 100644 index 0000000..824f313 --- /dev/null +++ b/0104-maximum-depth-of-binary-tree/step3.cpp @@ -0,0 +1,13 @@ +class Solution { +public: + int maxDepth(TreeNode* root) { + if (root == nullptr) { + return 0; + } + + int leftDepth = maxDepth(root->leftNode); + int rightDepth = maxDepth(root->rightNode); + + return max(leftDepth, rightDepth) + 1; + } +};