Skip to content

Conversation

@BrianLusina
Copy link
Owner

@BrianLusina BrianLusina commented Jan 2, 2026

Describe your change:

Algorithm to check if two values in a binary search tree add up to a target value.

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

Summary by CodeRabbit

  • New Features

    • Added comprehensive README coverage for the Two Sum BST variant with examples, diagrams, step-by-step solution, and complexity notes.
    • Introduced a tree-based level-order algorithm to detect target sum pairs in binary search trees.
  • Bug Fixes

    • Fixed pointer-based solution to correctly indicate when no matching pair is found.
  • Tests

    • Replaced individual tests with parameterized test suites for better coverage and organization.

✏️ Tip: You can customize this high-level summary in your review settings.

@BrianLusina BrianLusina self-assigned this Jan 2, 2026
@BrianLusina BrianLusina added enhancement Algorithm Algorithm Problem Datastructures Datastructures Documentation Documentation Updates Array Array data structure Binary Search Binary Search Algorithm Trees Binary Tree Queue Level Order Traversal labels Jan 2, 2026
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 2, 2026

Caution

Review failed

The pull request is closed.

📝 Walkthrough

Walkthrough

The PR adds a BST variant to the two-sum module with a new two_sum_find_target BFS-based function, updates README with the BST problem and solution, expands imports, ensures two_sum_with_pointers returns [] when no pair is found, and converts tests to parameterized cases including BST tests.

Changes

Cohort / File(s) Summary
Documentation
algorithms/arrays/two_sum/README.md
Added "Two Sum IV - Input Is a BST" section: problem statement, constraints, examples, BFS solution using a complement set, step-by-step algorithm, illustrations, and complexity (O(n) time, O(n) space).
Implementation
algorithms/arrays/two_sum/__init__.py
Added two_sum_find_target(root: BinaryTreeNode, k: int) -> bool (BFS with seen-set). Extended typing/imports (Deque, Set, deque, BinaryTreeNode). Ensured two_sum_with_pointers returns [] if no pair found.
Tests
algorithms/arrays/two_sum/test_two_sum.py
Replaced individual tests with parameterized tests and centralized test-case arrays (TWO_SUM_TEST_CASES, TWO_SUM_TWO_POINTERS_TEST_CASES, TWO_SUM_INPUT_BST_TEST_CASES). Added tests for two_sum_find_target using constructed BinaryTreeNode sequences; updated imports.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Poem

🐰 Upon tree branches I hop and peep,

I store complements while others sleep,
BFS steps light and spry,
Two sums found — a joyful cry,
Hops and carrots, tests pass neat.

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: adding a BST variant to the two-sum algorithm with clear focus on the key modification.
Description check ✅ Passed The description is well-structured, follows the template, clearly explains the change, and comprehensively addresses all checklist items except one unrelated issue-reference item.

📜 Recent review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2d4cfb5 and 3c1351c.

📒 Files selected for processing (1)
  • algorithms/arrays/two_sum/__init__.py

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
algorithms/arrays/two_sum/__init__.py (1)

67-104: Algorithm is correct. Consider a minor optimization.

The BFS implementation correctly finds pairs that sum to the target. The logic properly handles edge cases and prevents using the same node twice.

Optional improvement: Lines 100-101 enqueue children without checking for None, which adds unnecessary values to the queue. Consider this refinement:

🔎 Optional optimization to avoid enqueueing None values
         # if not, add the value
         seen.add(curr.data)
 
         # enqueue the left and right children of the current node
-        queue.append(curr.left)
-        queue.append(curr.right)
+        if curr.left:
+            queue.append(curr.left)
+        if curr.right:
+            queue.append(curr.right)
 
     # if we reach here, we have not found two nodes that add up to the target
     return False

This reduces queue pollution and eliminates the None check at line 91, making the BFS slightly more efficient.

📜 Review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7f2ad34 and 2d4cfb5.

⛔ Files ignored due to path filters (17)
  • algorithms/arrays/two_sum/images/examples/two_sum_4_input_is_bst_example_1.png is excluded by !**/*.png
  • algorithms/arrays/two_sum/images/examples/two_sum_4_input_is_bst_example_2.png is excluded by !**/*.png
  • algorithms/arrays/two_sum/images/examples/two_sum_4_input_is_bst_example_3.png is excluded by !**/*.png
  • algorithms/arrays/two_sum/images/solutions/two_sum_4_input_is_bst_solution_1.png is excluded by !**/*.png
  • algorithms/arrays/two_sum/images/solutions/two_sum_4_input_is_bst_solution_10.png is excluded by !**/*.png
  • algorithms/arrays/two_sum/images/solutions/two_sum_4_input_is_bst_solution_11.png is excluded by !**/*.png
  • algorithms/arrays/two_sum/images/solutions/two_sum_4_input_is_bst_solution_12.png is excluded by !**/*.png
  • algorithms/arrays/two_sum/images/solutions/two_sum_4_input_is_bst_solution_13.png is excluded by !**/*.png
  • algorithms/arrays/two_sum/images/solutions/two_sum_4_input_is_bst_solution_14.png is excluded by !**/*.png
  • algorithms/arrays/two_sum/images/solutions/two_sum_4_input_is_bst_solution_2.png is excluded by !**/*.png
  • algorithms/arrays/two_sum/images/solutions/two_sum_4_input_is_bst_solution_3.png is excluded by !**/*.png
  • algorithms/arrays/two_sum/images/solutions/two_sum_4_input_is_bst_solution_4.png is excluded by !**/*.png
  • algorithms/arrays/two_sum/images/solutions/two_sum_4_input_is_bst_solution_5.png is excluded by !**/*.png
  • algorithms/arrays/two_sum/images/solutions/two_sum_4_input_is_bst_solution_6.png is excluded by !**/*.png
  • algorithms/arrays/two_sum/images/solutions/two_sum_4_input_is_bst_solution_7.png is excluded by !**/*.png
  • algorithms/arrays/two_sum/images/solutions/two_sum_4_input_is_bst_solution_8.png is excluded by !**/*.png
  • algorithms/arrays/two_sum/images/solutions/two_sum_4_input_is_bst_solution_9.png is excluded by !**/*.png
📒 Files selected for processing (3)
  • algorithms/arrays/two_sum/README.md
  • algorithms/arrays/two_sum/__init__.py
  • algorithms/arrays/two_sum/test_two_sum.py
🧰 Additional context used
🧬 Code graph analysis (1)
algorithms/arrays/two_sum/test_two_sum.py (1)
algorithms/arrays/two_sum/__init__.py (3)
  • two_sum (6-32)
  • two_sum_with_pointers (35-64)
  • two_sum_find_target (67-104)
🪛 LanguageTool
algorithms/arrays/two_sum/README.md

[style] ~81-~81: Using many exclamation marks might seem excessive (in this case: 13 exclamation marks for a text that’s 3551 characters long)
Context: .../two_sum_4_input_is_bst_solution_1.png) Solution 2 Solution 3 Solution 4 Solution 5 Solution 6 Solution 7 Solution 8 Solution 9 Solution 10 Solution 11 Solution 12 ![Solution 13](./images/solutions/two_sum...

(EN_EXCESSIVE_EXCLAMATION)

🔇 Additional comments (7)
algorithms/arrays/two_sum/README.md (1)

38-110: LGTM! Comprehensive documentation for the BST variant.

The new section is well-structured with clear problem statement, constraints, examples, step-by-step algorithm, and complexity analysis. The markdown formatting and organization align well with the existing documentation style.

Note: The static analysis warning about excessive exclamation marks is a false positive—these are from markdown image syntax ![...], not prose.

algorithms/arrays/two_sum/__init__.py (2)

1-3: LGTM! Import changes support the new functionality.

The added imports (Deque, Set, deque, BinaryTreeNode) are all used in the new two_sum_find_target function.


64-64: Good defensive programming fix.

Adding an explicit return [] ensures the function contract is clear when no pair is found.

algorithms/arrays/two_sum/test_two_sum.py (4)

2-9: LGTM! Imports support the test refactoring.

The new imports enable parameterized testing and BST functionality testing, which improves test maintainability.


11-29: Well-structured test data.

Centralizing test cases as constants improves maintainability and makes it easy to add new test scenarios. The BST test cases cover good variety including single node, linear trees, and balanced trees.


33-43: Excellent refactoring to parameterized tests.

This approach eliminates code duplication and makes it easier to add new test cases. The type hints on test parameters enhance readability.


45-53: BST test implementation is correct.

The test properly constructs a BST using insert_node and filters out None values. The algorithm is tested with various tree structures, including edge cases like single-node trees and linear trees.

@BrianLusina BrianLusina merged commit 5a1e8e8 into main Jan 2, 2026
4 of 7 checks passed
@BrianLusina BrianLusina deleted the feat/algorithms-arrays-two-sum branch January 2, 2026 05:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Algorithm Algorithm Problem Array Array data structure Binary Search Binary Search Algorithm Binary Tree Datastructures Datastructures Documentation Documentation Updates enhancement Level Order Traversal Queue Trees

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants