Skip to content

Conversation

ryosuketc
Copy link
Owner

# self.right = right
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
stack = [(root, 1)]

Choose a reason for hiding this comment

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

nodes_and_depthsなどとしてもいい気がします

stack.append((node.right, depth + 1))

return max_depth

No newline at end of file

Choose a reason for hiding this comment

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

特に問題ないと思います。
再帰への書き換え、queueを使う/使わないBFS等の実装もありました


* https://github.com/nittoco/leetcode/pull/14/files
* https://discord.com/channels/1084280443945353267/1227073733844406343/1236235351140339742
* 私の方法は上から配る方だと思うが、概念として下から戻す方法が存在するのはわかるものの、今回どうやってやるのかよくわからない。このへん読んでみたが、結局なにをどうしているのかよくわからん…。一旦次に進む。
Copy link

Choose a reason for hiding this comment

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

先に進んでもいいかもしれません。一応、こちらに(別の問題ですが) Python のバージョンがあるので、これをみたらやろうとしていること分かるかもしれません。
potrue/leetcode#22 (comment)

Copy link
Owner Author

Choose a reason for hiding this comment

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

一応機械的に namedtuple で書き直してみましたが、結局あまりよくわかりませんでした。一旦先に進んで、どこかで戻ってみようかと思います… (return_value, left_value, right_value が何なのかすらわかっていない)。

class Solution:
    def minDepth(self, root: Optional[TreeNode]) -> int:
        if not root:
            return 0
            
        StackEntry = namedtuple('StackEntry', ['node', 'depth', 'return_value', 'left_value', 'right_value'])
        result = []
        stack = [StackEntry(root, 1, result, [], [])]
        while stack:
            node, depth, return_value, left_value, right_value = stack[-1]
            if node.left is not None and not left_value:
                stack.append(StackEntry(node.left, depth + 1, left_value, [], []))
                continue
            if node.right is not None and not right_value:
                stack.append(StackEntry(node.right, depth + 1, right_value, [], []))
                continue
            if not left_value and not right_value:
                min_depth = depth
            else:
                min_depth = min(value[0] for value in (left_value, right_value) if value)
            return_value.append(min_depth)
            stack.pop()
        return result[0]

Copy link
Owner Author

Choose a reason for hiding this comment

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

ここでの議論が参考になりそうなので記録しておきます。

https://github.com/ryosuketc/leetcode_arai60/pull/23/files#r2122113958

@ryosuketc ryosuketc added the comments reviewed Reviewe comments are examined. No major re-review of the problem is needed. label Jul 17, 2025
@ryosuketc ryosuketc merged commit 6c872b7 into main Jul 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comments reviewed Reviewe comments are examined. No major re-review of the problem is needed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants