Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified .DS_Store
Binary file not shown.
40 changes: 40 additions & 0 deletions 104.MaximumDepthofBinaryTree/bfs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
struct NodeAndDepth {
TreeNode* node;
int depth;
};

class Solution {
public:
int maxDepth(TreeNode* root) {
if (root == nullptr) {
return 0;
}

queue<NodeAndDepth> candidates;
candidates.push({root, 1});
int max_depth = 0;
while (!candidates.empty()) {
const auto [node, depth] = candidates.front();
candidates.pop();
if (node == nullptr) {
continue;
}
max_depth = max(depth, max_depth);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

必ず、depth >= max_depthではないですか?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

私もここは常にdepthのほうが大きいと思います.

max_depth = depth

でも同じことになると思います.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@liquo-rice @fhiyo
常に更新されているので意味無かったですね。修正しました。

candidates.push({node->left, depth + 1});
candidates.push({node->right, depth + 1});
}

return max_depth;
}
};
40 changes: 40 additions & 0 deletions 104.MaximumDepthofBinaryTree/bfs_step2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
struct NodeAndDepth {
TreeNode* node;
int depth;
};

class Solution {
public:
int maxDepth(TreeNode* root) {
if (root == nullptr) {
return 0;
}

queue<NodeAndDepth> candidates;
candidates.push({root, 1});
int max_depth = 0;
while (!candidates.empty()) {
const auto [node, depth] = candidates.front();
candidates.pop();
if (node == nullptr) {
continue;
}
max_depth = depth;
candidates.push({node->left, depth + 1});
candidates.push({node->right, depth + 1});
}

return max_depth;
}
};
42 changes: 42 additions & 0 deletions 104.MaximumDepthofBinaryTree/bfs_step3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int maxDepth(TreeNode* root) {
if (!root) {
return 0;
}

queue<NodeAndDepth> node_and_depth;
node_and_depth.push({root, 1});
int max_dempth = 0;
while (!node_and_depth.empty()) {
auto [node, depth] = node_and_depth.front();
node_and_depth.pop();

max_dempth = max(max_dempth, depth);
if (node->left) {
node_and_depth.push({node->left, depth + 1});
}
if (node->right) {
node_and_depth.push({node->right, depth + 1});
}
}
return max_dempth;
}

private:
struct NodeAndDepth {
TreeNode* node;
int depth;
};
};
40 changes: 40 additions & 0 deletions 104.MaximumDepthofBinaryTree/memo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
## ステップ1
階層が深くなるごとに数値を足していって、再帰でnullptrになるまで潜れば解けそう
関数化したけどこの長さなら関数化する必要ないかもだけど
もっと複雑化することを想定して今回は分けてみた

時間計算量
深さに影響され、全て左に紐づく場合を考慮してO(n)

空間計算量
O(n)

## ステップ2
rootがnullptrとなっている場合を考慮してstep2で追加

return 1+max(maxDepth(root->left),maxDepth(root->right));のようにも書けるが
若干可読性が下がると感じた。
https://leetcode.com/problems/maximum-depth-of-binary-tree/solutions/3192154/cpp-solve-easy-solve/

## 他の解法
・BFSを使っても解くことができる bfs.cppに実装
structで深さとnodeを持つ構造を定義
命名ルールは下記参照
https://google.github.io/styleguide/cppguide.html#Type_Names

参考にした回答
同階層をforループで回すよりは、階層ごとの深さをもつ方が良さそう
https://github.com/rossy0213/leetcode/pull/10/commits/827bf9f2ed20d4c647561fe9a0d73b761d04564e

popしてからnodeを確認する方法がある。
https://github.com/NobukiFukui/Grind75-ProgrammingTraining/pull/38
これをすることで左と右それぞれ確認してからqueueに追加する必要がなくなり
シンプルに書くことができる

・「この環境では大丈夫」なコードは可能ならば避ける
https://github.com/kazukiii/leetcode/pull/22/commits/f2d8eedba1e98a8da5c97093224cca35326e7cca#diff-e6fac96b6faeb56aacc2da73ab20276cbd0c1c118797522d3a88bd5d271b384c

https://github.com/rossy0213/leetcode/pull/10/commits/827bf9f2ed20d4c647561fe9a0d73b761d04564e

## Discorなど

28 changes: 28 additions & 0 deletions 104.MaximumDepthofBinaryTree/step1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int MeasureDepth(TreeNode* node) {
if (node == nullptr) {
return 0;
}
int left_depth = MeasureDepth(node->left);
int right_depth = MeasureDepth(node->right);

return max(left_depth, right_depth) + 1;
}

int maxDepth(TreeNode* root) {
int depth = MeasureDepth(root);
return depth;
}
};
31 changes: 31 additions & 0 deletions 104.MaximumDepthofBinaryTree/step2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int MeasureDepth(TreeNode* node) {
if (node == nullptr) {
return 0;
}
int left_depth = MeasureDepth(node->left);
int right_depth = MeasureDepth(node->right);

return max(left_depth, right_depth) + 1;
}

int maxDepth(TreeNode* root) {
if (root == nullptr) {
return 0;
}
Comment on lines +25 to +27
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

root == nullptrを特別扱いする必要がないように見えます。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fhiyo
関数側でチェックしているので必要ないですね。今回は他の指摘であったことから関数自体削除しましたのでメイン処理側では残しました。

int depth = MeasureDepth(root);
return depth;
}
};
31 changes: 31 additions & 0 deletions 104.MaximumDepthofBinaryTree/step3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int MeasureDepth(TreeNode* node) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この関数を公開する必要がないように見えました。privateでいいのかなと思います。

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

メンバー変数使わないのと、引数のノードも変更しないので、以下のように書けます。

static int MeasureDepth(TreeNode* node)

また、maxDepthとMeasureDepthは、呼び出し側からすると完全に同じ振る舞いをします。一つにまとめて良さそうです。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fhiyo @liquo-rice
レビューありがとうございます。staticを使うことも覚えておきます。
指摘の通り分ける必要ないので今回は一つに纏めました。なのでMeasureDepth関数自体削除しました。

94e08f1

if (node == nullptr) {
return 0;
}
int left_depth = MeasureDepth(node->left);
int right_depth = MeasureDepth(node->right);

return max(left_depth, right_depth) + 1;
}

int maxDepth(TreeNode* root) {
if (root == nullptr) {
return 0;
}
int depth = MeasureDepth(root);
return depth;
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この関数、単に return MeasureDepth(root); でよくないでしょうか。としたら、二つを一つにしてもいいのではないでしょうか。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@oda
レビューありがとうございます。直近の問題のほとんどを関数化していたので、関数化してしまいましたが
liquoさんからも指摘あったように振る舞いは変わらないので分ける必要ないですね。
step4にて修正しました。
94e08f1

};
23 changes: 23 additions & 0 deletions 104.MaximumDepthofBinaryTree/step4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int maxDepth(TreeNode* root) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leetcode の都合上変えられないので仕方ないですが、引数に渡される TreeNode がルートノードなのは最初だけなので、これを root とするのは適当でないように思いました。

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

各関数呼び出しを考えると、そのnodeを根とするツリーのmaxDepthを求めているという意味ではありなのかなと思います

その場合、left_tree_max_depth, right_tree_max_depthと変数名をつけることが適当でしょうか(長いか)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

各関数呼び出しを考えると、そのnodeを根とするツリーのmaxDepthを求めているという意味ではありなのかなと思います

あーなるほどその発想はありませんでした。そう考えると確かに不自然ではないかもしれないです。

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

その場合、left_tree_max_depth, right_tree_max_depthと変数名をつけることが適当でしょうか(長いか)

Java ならまあありそうだなって感じの長さに思いました。 C++ だとどうなんでしょうね

if (root == nullptr) {
return 0;
}
int left_depth = maxDepth(root->left);
int right_depth = maxDepth(root->right);

return max(left_depth, right_depth) + 1;
}
};
28 changes: 28 additions & 0 deletions 104.MaximumDepthofBinaryTree/step5.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int maxDepth(TreeNode* root) {
if (!root) {
return 0;
}
return FindMaxDepth(root);
}

private:
int FindMaxDepth(TreeNode* node) {
if (!node) {
return 0;
}
return max(FindMaxDepth(node->left), FindMaxDepth(node->right)) + 1;
}
};
20 changes: 20 additions & 0 deletions 104.MaximumDepthofBinaryTree/step6.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int maxDepth(TreeNode* node) {
if (!node) {
return 0;
}
return max(maxDepth(node->left), maxDepth(node->right)) + 1;
}
};
20 changes: 20 additions & 0 deletions problem_name/step1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int maxDepth(TreeNode* node) {
if (!node) {
return 0;
}
return max(maxDepth(node->left), maxDepth(node->right)) + 1;
}
};