-
Notifications
You must be signed in to change notification settings - Fork 0
solved binary_tree_level_order_traversal #42
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: arai60
Are you sure you want to change the base?
Conversation
| if len(node_ids) > 0: | ||
| level_to_nodes.append(node_ids) |
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.
単に if node_ids: でもいいでしょう。
個人的には current_level_nodes = next_level_nodes の前に置きたいです。
current_level_nodes に None が入っているので、これいるんですね。よしあしですが、入るかもしれない前提で書いたほうが単純かもしれません。
| current_level_nodes = [root] | ||
| while current_level_nodes: | ||
| next_level_nodes = [] | ||
| node_ids = [] |
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.
問題文の単語に合わせて、node_vals としてもよいように感じました。他の単語に合わせて current_level_vals とかでもよいかもしれません。
| # self.right = right | ||
| class Solution: | ||
| def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]: | ||
| level_to_nodes = [] |
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.
こちら、階層ごとの値が入っているというよりは、階層とそれに紐づく node が入っているように見えました。level_order_vals や all_level_vals が浮かびました。
| class Solution: | ||
| def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]: | ||
| level_traversed_node_ids = [] | ||
| def append_by_depth(node: Optional[TreeNode], depth: int): |
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.
自分ならids -> vals、depth -> levelを使うと思います。
表現にバリエーションが出てしまうと、後々混乱の原因になるかもと思いました。
問題: https://leetcode.com/problems/binary-tree-level-order-traversal/description/?envType=problem-list-v2&envId=xo2bgr0r