Skip to content

Conversation

@Ryotaro25
Copy link
Owner

問題へのリンク
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内に各ステップで感じたことを追記します。

Comment on lines +14 to +16
bool isSameTree(TreeNode* p, TreeNode* q) {
return IsIdenticalTree(p, q);
}
Copy link

Choose a reason for hiding this comment

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

同じなので一つにしてもいいですね。

Copy link
Owner Author

Choose a reason for hiding this comment

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

レビューありがとうございます。
pとqの名前を変えたかっただけですので、step2で一つしました。

Copy link

Choose a reason for hiding this comment

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

C++ のシグネチャーの変数名は変わっても同じものとして扱われるので、変えてしまっても構わないでしょう。
Python だと、キーワード引数による呼び出しがあるのでそうはいきませんが。

Copy link
Owner Author

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) {
Copy link

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()) {
Copy link

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)) {
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants