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; + } +};