Skip to content

Conversation

@SuperHotDogCat
Copy link
Owner

Comment on lines +21 to +22
if len(node_ids) > 0:
level_to_nodes.append(node_ids)
Copy link

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 = []

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 = []

Choose a reason for hiding this comment

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

こちら、階層ごとの値が入っているというよりは、階層とそれに紐づく node が入っているように見えました。level_order_valsall_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):

Choose a reason for hiding this comment

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

自分ならids -> valsdepth -> levelを使うと思います。
表現にバリエーションが出てしまうと、後々混乱の原因になるかもと思いました。

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.

5 participants