-
Notifications
You must be signed in to change notification settings - Fork 0
100. Same Tree #70
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?
100. Same Tree #70
Conversation
| bool isSameTree(TreeNode* p, TreeNode* q) { | ||
| return IsIdenticalTree(p, q); | ||
| } |
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.
同じなので一つにしてもいいですね。
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.
レビューありがとうございます。
pとqの名前を変えたかっただけですので、step2で一つしました。
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.
C++ のシグネチャーの変数名は変わっても同じものとして扱われるので、変えてしまっても構わないでしょう。
Python だと、キーワード引数による呼び出しがあるのでそうはいきませんが。
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.
こういったところにも言語による差があるのですね。
この会でよくみるPythonやJavaについてはもう少し理解しようと思います。
| */ | ||
| class Solution { | ||
| public: | ||
| bool isSameTree(TreeNode* p, TreeNode* q) { |
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.
p = [1, 2], q = [2, null, 1] に対して誤って true を返す気がしました。
| vector<int> second_tree_nums; | ||
| PushNumsByInorder(second_tree_nums, q); | ||
|
|
||
| if (first_tree_nums.size() != second_tree_nums.size()) { |
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.
return first_tree_nums.size() == second_tree_nums; のほうがシンプルだと思いました。
| auto [node, other_node] = node_pairs.front(); | ||
| node_pairs.pop(); | ||
|
|
||
| if (!IsSameNode(node, other_node)) { |
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.
IsSameNode() が同じノードに対して複数回呼ばれている点が無駄に感じました。
class Solution {
public:
bool isSameTree(TreeNode* p, TreeNode* q) {
queue<NodePair> node_pairs;
node_pairs.push({p, q});
while (!node_pairs.empty()) {
auto [node, other_node] = node_pairs.front();
node_pairs.pop();
if (!IsSameNode(node, other_node)) {
return false;
}
if (node && other_node) {
node_pairs.push({node->left, other_node->left});
node_pairs.push({node->right, other_node->right});
}
}
return true;
}
private:
struct NodePair {
TreeNode* node;
TreeNode* other_node;
};
bool IsSameNode (TreeNode* node, TreeNode* other_node) {
if (!node && !other_node) {
return true;
}
if (!node || !other_node) {
return false;
}
return node->val == other_node->val;
}
};
問題へのリンク
https://leetcode.com/problems/same-tree/description/
問題文(プレミアムの場合)
備考
次に解く問題の予告
Invert/Flip Binary Tree
blind75のtree周りを先に解きます。
フォルダ構成
LeetCodeの問題ごとにフォルダを作成します。
フォルダ内は、step1.cpp、step1_wa.cpp、step2.cpp、step2_2.cpp、step3.cppとmemo.mdとなります。
memo.md内に各ステップで感じたことを追記します。