-
Notifications
You must be signed in to change notification settings - Fork 0
102. Binary Tree Level Order Traversal #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| ### step1 | ||
|
|
||
| dfsで各ノードを走査しながら、もっている各階層のlevelのvector[level]にnodeをpush_backしていく。 | ||
|
|
||
| ### step2 | ||
|
|
||
| 名前がてきとうだったのでましな名前に変更。 | ||
|
|
||
| ### step3 | ||
|
|
||
| ChatGPTに聞いたところ、「👉 レベル順探索 = BFSが本質的に一致」ということがわかったのでBFSで実装し直し。 | ||
| 3回通すまで書き直し。 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| /** | ||
| * 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: | ||
| void dfs(TreeNode* root, int level, vector<vector<int>>& vvi) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 関数名は、動詞の原形または命令形で始まることが多いように思います。 traverse() はいかがでしょうか? |
||
| if (!root) { | ||
| return; | ||
| } | ||
| if (vvi.size() < (level + 1)) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (level + 1) の () は冗長に感じました。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. そうですね、括弧なしにします。 |
||
| vvi.resize(level + 1); | ||
| } | ||
| vvi[level].push_back(root->val); | ||
| if (root->left) { | ||
| dfs(root->left, level + 1, vvi); | ||
| } | ||
| if (root->right) { | ||
| dfs(root->right, level + 1, vvi); | ||
| } | ||
| } | ||
|
|
||
| vector<vector<int>> levelOrder(TreeNode* root) { | ||
| int level = 0; | ||
| vector<vector<int>> vvi; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. とりあえず付けたてきとうな名前ですね。一応 vector<vector< int >> を短くしてvviみたいな感じです。step2でnodesという名前にrenameしています。 |
||
| dfs(root, level, vvi); | ||
| return vvi; | ||
| } | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| /** | ||
| * 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: | ||
| void scan(TreeNode* root, int level, vector<vector<int>>& nodes) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. scan という英単語は、一列に並んでいるものを走査するというニュアンスを感じます。グラフ構造を辿っていくのであれば traverse() あたりが良いと思います。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. そうなんですね。traverseの方を使うようにします。 |
||
| if (!root) { | ||
| return; | ||
| } | ||
| if (nodes.size() < (level + 1)) { | ||
| nodes.resize(level + 1); | ||
| } | ||
| nodes[level].push_back(root->val); | ||
| if (root->left) { | ||
| scan(root->left, level + 1, nodes); | ||
| } | ||
| if (root->right) { | ||
| scan(root->right, level + 1, nodes); | ||
| } | ||
| } | ||
|
|
||
| vector<vector<int>> levelOrder(TreeNode* root) { | ||
| int level = 0; | ||
| vector<vector<int>> nodes; | ||
| scan(root, level, nodes); | ||
| return nodes; | ||
| } | ||
| }; | ||
| 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) {} | ||
| * }; | ||
| */ | ||
| class Solution { | ||
| public: | ||
| vector<vector<int>> levelOrder(TreeNode* root) { | ||
| vector<vector<int>> result; | ||
| if (!root) return result; | ||
|
|
||
| queue<TreeNode*> nodes; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. kitano-kazuki/leetcode#21 (comment)
BFSでは, 階層ごとに配列を用意する方法があります。最近は読みやすいので自分もこっちを使っています There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @hemispherium さん
という書き方を最初に見たのはどこか覚えていますか? |
||
| nodes.push(root); | ||
|
|
||
| while (!nodes.empty()) { | ||
| int size = nodes.size(); | ||
| vector<int> level; | ||
|
|
||
| for (int i = 0; i < size; i++) { | ||
| TreeNode* node = nodes.front(); | ||
| nodes.pop(); | ||
|
|
||
| level.push_back(node->val); | ||
|
|
||
| if (node->left) nodes.push(node->left); | ||
| if (node->right) nodes.push(node->right); | ||
| } | ||
|
|
||
| result.push_back(level); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
vvi という変数名は vector vector int の略でしょうか。変数名から、中にどのような値が含まれているのか想像しにくく感じました。
こちらのコメントをご参照ください。
#10 (comment)
また、型名をそのまま変数名に含めても、読み手にとって得られる情報はあまり増えないように感じました。変数名には、どのような値が格納されているかが分かる名前を付けると、より読みやすくなると思います。
nodes_by_level はいかがでしょうか?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
そうですね、承知しました。nodes_by_levelのような名前を付けるようにします。