- Notifications
You must be signed in to change notification settings - Fork 2
feat(datastructures, binary-tree): zig zag level order traversal #103
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
feat(datastructures, binary-tree): zig zag level order traversal #103
Conversation
| Caution Review failedThe pull request is closed. WalkthroughA new public method Changes
Sequence Diagram(s)sequenceDiagram participant Client participant BinaryTree participant Queue participant Result Client->>BinaryTree: zig_zag_level_order() activate BinaryTree BinaryTree->>Queue: enqueue(root) if root exists loop per level BinaryTree->>Queue: dequeue all nodes at current level BinaryTree->>BinaryTree: collect node list for level alt odd level BinaryTree->>Result: append reversed node list else even level BinaryTree->>Result: append node list as-is end BinaryTree->>Queue: enqueue children of current level end BinaryTree-->>Client: return Result (List[List[BinaryTreeNode]]) deactivate BinaryTree Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes
Possibly related PRs
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
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.
Actionable comments posted: 3
🧹 Nitpick comments (1)
datastructures/trees/binary/tree/__init__.py (1)
653-653: Consider tightening the type annotation.The type annotation allows
Nonefor theBinaryTreeNode, but in practice, the first element of tuples in the stack is neverNonebecause:
- The initial element is
self.root, which is guaranteed non-Noneby the guard on line 649- All subsequent appends (lines 659, 661, 664, 666, 670, 672) check that
node.leftornode.rightis notNonebefore appendingConsider applying this diff to tighten the type:
- stack: List[Tuple[BinaryTreeNode | None, int, str | None]] = [(self.root, 0, None)] + stack: List[Tuple[BinaryTreeNode, int, str | None]] = [(self.root, 0, None)]
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
datastructures/trees/binary/tree/__init__.py(3 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
datastructures/trees/binary/tree/__init__.py (1)
datastructures/trees/binary/node.py (1)
BinaryTreeNode(6-184)
🔇 Additional comments (2)
datastructures/trees/binary/tree/__init__.py (2)
2-2: LGTM!The
Tupleimport is correctly added to support the type annotation on line 653.
677-677: Verify the return type choice.The method returns
List[List[BinaryTreeNode]](lists of node objects), which differs fromlevel_order_traversal(lines 85-107) that returnsList[Any]containing node values (node.data).Most standard zigzag level-order traversal implementations return node values rather than node objects. Please confirm this design choice is intentional.
If values are intended instead of nodes, apply this diff:
- def zig_zag_level_order(self) -> List[List[BinaryTreeNode]]: + def zig_zag_level_order(self) -> List[List[T]]: """ Perform zigzag level order traversal of a binary tree. Returns: - List[List[BinaryTreeNode]] - zigzag level order traversal + List[List[T]] - zigzag level order traversal of node valuesAnd update line 707:
- current_level.append(node) + current_level.append(node.data)
Describe your change:
Adds zig zag level order traversal algorithm
Checklist:
Fixes: #{$ISSUE_NO}.Summary by CodeRabbit