-
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| # Definition for a binary tree node. | ||
| # class TreeNode: | ||
| # def __init__(self, val=0, left=None, right=None): | ||
| # self.val = val | ||
| # self.left = left | ||
| # self.right = right | ||
| class Solution: | ||
| def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]: | ||
| level_traversed_node_ids = [] | ||
| def append_by_depth(node: Optional[TreeNode], depth: int): | ||
| if not node: | ||
| return | ||
| if len(level_traversed_node_ids) == depth: | ||
| level_traversed_node_ids.append([]) | ||
|
|
||
| level_traversed_node_ids[depth].append(node.val) | ||
| append_by_depth(node.left, depth + 1) | ||
| append_by_depth(node.right, depth + 1) | ||
|
|
||
| append_by_depth(root, 0) | ||
| return level_traversed_node_ids | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| # レベルごとの捜査なのでBFSが適しているというコメントがあり, そりゃそうだわの気持ちになり反省 | ||
| # 再帰で書く変な先入観みたいなのがついている気がしたので今一度なんで書くのかをしっかりと... | ||
| # seal-azarashi: https://github.com/seal-azarashi/leetcode/pull/25/files | ||
| # goto-untrapped: https://github.com/goto-untrapped/Arai60/pull/50/files | ||
| # Ryotaro25: https://github.com/Ryotaro25/leetcode_first60/pull/28/files | ||
| # BFSで解くこと, 一つの変数に二つの役割を持たさないように注意して再度実行 | ||
|
|
||
| # Definition for a binary tree node. | ||
| # class TreeNode: | ||
| # def __init__(self, val=0, left=None, right=None): | ||
| # self.val = val | ||
| # self.left = left | ||
| # 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 commentThe reason will be displayed to describe this comment to others. Learn more. こちら、階層ごとの値が入っているというよりは、階層とそれに紐づく node が入っているように見えました。 |
||
| 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 commentThe reason will be displayed to describe this comment to others. Learn more. 問題文の単語に合わせて、 |
||
| for node in current_level_nodes: | ||
| if not node: | ||
| continue | ||
| node_ids.append(node.val) | ||
| next_level_nodes.append(node.left) | ||
| next_level_nodes.append(node.right) | ||
|
|
||
| current_level_nodes = next_level_nodes | ||
| if len(node_ids) > 0: | ||
| level_to_nodes.append(node_ids) | ||
|
|
||
| return level_to_nodes | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| # Definition for a binary tree node. | ||
| # class TreeNode: | ||
| # def __init__(self, val=0, left=None, right=None): | ||
| # self.val = val | ||
| # self.left = left | ||
| # self.right = right | ||
| class Solution: | ||
| def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]: | ||
| level_to_nodes = [] | ||
| current_level_nodes = [root] | ||
| while current_level_nodes: | ||
| node_ids = [] | ||
| next_level_nodes = [] | ||
| for node in current_level_nodes: | ||
| if not node: | ||
| continue | ||
| node_ids.append(node.val) | ||
| next_level_nodes.append(node.left) | ||
| next_level_nodes.append(node.right) | ||
| current_level_nodes = next_level_nodes | ||
| if len(node_ids) > 0: | ||
| level_to_nodes.append(node_ids) | ||
|
Comment on lines
+21
to
+22
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 単に if node_ids: でもいいでしょう。 current_level_nodes に None が入っているので、これいるんですね。よしあしですが、入るかもしれない前提で書いたほうが単純かもしれません。 |
||
|
|
||
| return 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.
自分なら
ids -> vals、depth -> levelを使うと思います。表現にバリエーションが出てしまうと、後々混乱の原因になるかもと思いました。