-
Notifications
You must be signed in to change notification settings - Fork 0
103. Binary Tree Zigzag Level Order Traversal #41
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
Open
skypenguins
wants to merge
1
commit into
main
Choose a base branch
from
leetcode/arai60/problem-103
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| # 103. Binary Tree Zigzag Level Order Traversal | ||
| - 問題: https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ | ||
| - 言語: Python | ||
|
|
||
| ## Step1 | ||
| - 二分木を階層ごとにグループ化した値をジグザグに返す | ||
| ### 方針 | ||
| - 実行時間の見積: およそ 0.2ms (102と同じ) | ||
| - 大まかな方針は102. Binary Tree Level Order Traversalと同じだが、今回は偶数番目の階層の要素の順序を逆転させる。 | ||
|
|
||
| ### AC | ||
| ```py | ||
| # 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 zigzagLevelOrder(self, root: Optional[TreeNode]) -> List[List[int]]: | ||
| if root is None: | ||
| return [] | ||
|
|
||
| zigzag_level = [] | ||
| pending_nodes = deque([root]) | ||
|
|
||
| while pending_nodes: | ||
| current_level = [] | ||
|
|
||
| for _ in range(len(pending_nodes)): | ||
| node = pending_nodes.popleft() | ||
| current_level.append(node.val) | ||
|
|
||
| if node.left: | ||
| pending_nodes.append(node.left) | ||
|
|
||
| if node.right: | ||
| pending_nodes.append(node.right) | ||
|
|
||
| if len(zigzag_level) % 2 != 0: | ||
| zigzag_level.append(list(reversed(current_level))) | ||
| continue | ||
| zigzag_level.append(current_level) | ||
|
|
||
| return zigzag_level | ||
| ``` | ||
| - 所要時間: 10:05 | ||
| - `while pending_nodes` を `while pending_nodes is not None` とするとMLEとなった | ||
| - `deque` オブジェクトは空になっても `None` にはならないため、`is not None` は常に `True` であるため | ||
|
|
||
| ## Step2 | ||
| - 典型コメント集: https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.iwb0z9gj4fve | ||
| - なし | ||
|
|
||
| - https://github.com/nicah4o/arai60/pull/26 | ||
| - C++ | ||
| - 節の値を詰める方向を交互に変更する方針 | ||
|
|
||
| - https://github.com/hiroki-horiguchi-dev/leetcode/pull/27 | ||
| - Java | ||
| - 同様に節の値を詰める方向を交互に変更する | ||
| - 若干、こちらの方がメモリ使用量は少ないかもしれない | ||
|
|
||
| ## Step3 | ||
| ### 読みやすく書き直したコード | ||
| ```py | ||
| # 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 zigzagLevelOrder(self, root: Optional[TreeNode]) -> List[List[int]]: | ||
| if root is None: | ||
| return [] | ||
|
|
||
| zigzag_levels = [] | ||
| pending_nodes = deque([root]) | ||
|
|
||
| while len(pending_nodes) != 0: | ||
| current_level = [] | ||
|
|
||
| for _ in range(len(pending_nodes)): | ||
| node = pending_nodes.popleft() | ||
| current_level.append(node.val) | ||
|
|
||
| if node.left: | ||
| pending_nodes.append(node.left) | ||
|
|
||
| if node.right: | ||
| pending_nodes.append(node.right) | ||
|
|
||
| if len(zigzag_levels) % 2 != 0: | ||
| zigzag_levels.append(list(reversed(current_level))) | ||
| continue | ||
|
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-elseで書くほうが分かりやすいと感じました。
Owner
Author
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-elseで書くか迷いました。 |
||
| zigzag_levels.append(current_level) | ||
|
|
||
| return zigzag_levels | ||
| ``` | ||
| - 所要時間: | ||
| - 1回目: 4:14 | ||
| - 2回目: 3:04 | ||
| - 3回目: 4:59 | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
while 文が終わったあとに、奇数行だけ反転させるという方法もあります。
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.
ありがとうございます。確かにwhile文を抜けた後でもできますね。