Skip to content

112. Path Sum#24

Open
seal-azarashi wants to merge 6 commits intomainfrom
path-sum
Open

112. Path Sum#24
seal-azarashi wants to merge 6 commits intomainfrom
path-sum

Conversation

@seal-azarashi
Copy link
Copy Markdown
Owner

Copy link
Copy Markdown

@fhiyo fhiyo left a comment

Choose a reason for hiding this comment

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

いいと思います!

Comment on lines +71 to +73
if (isLeafNode && root.val == targetSum) {
return true;
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

        if (isLeafNode) {
            return  root.val == targetSum;
        }

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.

step 4 に修正版を書きました

Comment on lines +105 to +107
if (isLeafNode && currentPathSum == targetSum) {
return true;
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

上と同じく、葉ノードならここでリターンするのが自然な気がします。

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.

こちらの場合は currentPathSum == targetSum が false の場合は処理を継続しなければならないので、こんな感じでしょうか

            if (isLeafNode) {
                if (currentPathSum == targetSum) {
                    return true;
                }
                
                continue;
            }

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

そうです!

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.

ありがとうございます!こちらも step 4 に記載しました

if (root == null) {
return false;
}
boolean isLeafNode = root.left == null && root.right == null;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

私はこれを変数には置かない気がします。ただ趣味の範囲です。

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.

返信遅くなりました。
確認するのに時間が空いたからか、改めて見ると冗長に感じたので、変数に置かないように step 4 を修正しました。

}

if (node.right != null) {
nodeStack.push(new NodeWithPathSum(node.right, currentPathSum));
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Java は最近(Tiger 以降)ほとんど書いていないのですが、record は null 入らないんでしたっけ。スタックには null も入るようにして、チェックを pop してから行うのも一つかと思いました。

Copy link
Copy Markdown
Owner Author

@seal-azarashi seal-azarashi Oct 15, 2024

Choose a reason for hiding this comment

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

返信遅くなりました。
Null が入らないのは ArrayDeque の方になります。 Record はイミュータブルですが null を代入することは可能です。
確かに、別のクラスを使って、チェックを pop してから行うのも一つですが、経験上 null を許容しない設計になっているクラス (メンバ etc.) ないしコレクションの方が扱いやすかったので、こちらをあえて選んでいました。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

あれ、ArrayDeque は、null が入らないのはいいですが、null を要素に持つ Record も入らないということですか?
それとも、文法上はTreeNode node = nodeWithPathSum.node;の直後に null check をして、あと3箇所のチェックを消すというのも選択として可能ということですか。

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.

あ、すいません読み違えてました。Null であるフィールドを持つ record は許容されます (null だとだめなのは record のインスタンスでした)。ですので、スタックには null も入るようにして、チェックを pop してから行うのは可能です。

こんな実装になります (step 4 に記載しました)

class Solution {
    private record NodeWithPathSum(TreeNode node, int pathSum) {};

    public boolean hasPathSum(TreeNode root, int targetSum) {
        if (root == null) {
            return false;
        }

        Deque<NodeWithPathSum> nodeStack = new ArrayDeque<>();
        nodeStack.push(new NodeWithPathSum(root, 0));
        while (!nodeStack.isEmpty()) {
            NodeWithPathSum nodeWithPathSum = nodeStack.pop();
            TreeNode node = nodeWithPathSum.node;

            if (node == null) {
                continue;
            }

            int currentPathSum = node.val + nodeWithPathSum.pathSum;
            if (node.left == null && node.right == null) {
                if (currentPathSum == targetSum) {
                    return true;
                }
                
                continue;
            }
            nodeStack.push(new NodeWithPathSum(node.right, currentPathSum));
            nodeStack.push(new NodeWithPathSum(node.left, currentPathSum));
        }
        return false;
    }
}

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.

こちらの方が見通しが良く感じました。

seal_azarashi added 2 commits October 16, 2024 07:26
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