Skip to content

103. Binary Tree Zigzag Level Order Traversal#26

Open
tom4649 wants to merge 2 commits intomainfrom
103.Binary-Tree-Zigzag-Level-Order-Traversal
Open

103. Binary Tree Zigzag Level Order Traversal#26
tom4649 wants to merge 2 commits intomainfrom
103.Binary-Tree-Zigzag-Level-Order-Traversal

Conversation

@tom4649
Copy link
Copy Markdown
Owner

@tom4649 tom4649 commented Mar 17, 2026

@huyfififi
Copy link
Copy Markdown

Level (同じ深さ) ごとに値を集めると考えると、BFSの方がDFSよりも素直な気がします。

103/sol1_bfs.py Outdated
if depth % 2 == 0:
res[depth].append(node.val)
else:
res[depth].insert(0, node.val)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

list の先頭や途中に対して insert をすると、それより後の要素をひとつづつずらさなければならないため、 O(n) かかります。原則避けたほうが無難だと思います。要素数が十分に小さいのであれば問題ないかもしれません。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

なるほど、要素数が多い場合にはresをdequeのlistとして用意し、最後にlistのlistに変換した方が計算量が小さくなるかもしれません。

103/sol1_bfs.py Outdated
Comment on lines +14 to +16
res: List[List[int]] = []

def dfs(node: Optional[TreeNode], depth: int) -> None:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

res とか dfs とか、命名が雑な印象を受けました。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

「自分が読めればいい」の癖が抜けないので、注意します。

103/sol1_bfs.py Outdated
Comment on lines +20 to +21
if len(res) == depth:
res.append([])
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

別な問題に対するコメントですが、こちらにも共通しそうなので。
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.hmkwp3k9djib

1段だけしか拡張しなくても例外が投げられることがないことに気がつくパズルを解かせる必要ないですよね。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

なるほど。whileで書いた方が読みやすいですね

103/sol1_bfs.py Outdated
dfs(node.left, depth + 1)
dfs(node.right, depth + 1)

dfs(root, 0)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

rootのdepthは1であるのが自然に思いました(0-indexです、と言われればそうかあ、とも思いますが…)。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

うーん、ここは0のままにしようと思います。0-indexなら不自然ではないと思います。

103/sol1_bfs.py Outdated
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ファイル名は bfs ですが、DFSですね。

103/sol1_bfs.py Outdated
Comment on lines +14 to +16
res: List[List[int]] = []

def dfs(node: Optional[TreeNode], depth: int) -> None:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

103/sol1_bfs.py Outdated

class Solution:
def zigzagLevelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
res: List[List[int]] = []
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pythonだとレキシカルスコープで再帰を回せるので,関数のシグネチャが長くならず,便利ですね.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

確かC++とかは引数を渡す必要がありましたね。
余裕があったらPython以外の言語も学ぼうと思います。

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