Skip to content

completed trees - 3#1590

Open
yashhh-23 wants to merge 1 commit into
super30admin:masterfrom
yashhh-23:master
Open

completed trees - 3#1590
yashhh-23 wants to merge 1 commit into
super30admin:masterfrom
yashhh-23:master

Conversation

@yashhh-23

Copy link
Copy Markdown

No description provided.

Copilot AI review requested due to automatic review settings July 8, 2026 11:37

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Adds Java binary-tree path-sum solution code (DFS + backtracking) to the repository.

Changes:

  • Added a pathSum (Path Sum II) implementation using recursion, path tracking, and backtracking.
  • Added the same implementation in two separate files (symmetrictree.java and pathsum2.java).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 6 comments.

File Description
symmetrictree.java Adds a DFS/backtracking pathSum solution (currently duplicated and file name does not match implementation).
pathsum2.java Adds a DFS/backtracking pathSum solution (same implementation as symmetrictree.java).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread symmetrictree.java
* }
* }
*/
class Solution {
Comment thread symmetrictree.java
Comment on lines +24 to +27
public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
this.result = new ArrayList<>();
helper(root, targetSum, 0, new ArrayList<>());
return result;
Comment thread symmetrictree.java
Comment on lines +1 to +5
// Time Complexity : O(n)
// Space Complexity : O(n) - for the recursion stack
// Did this code successfully run on Leetcode : yes
// Any problem you faced while coding this : yes writing logic for backtracking and keeping track of the current sum and path

Comment thread pathsum2.java
Comment on lines +1 to +5
// Time Complexity : O(n)
// Space Complexity : O(n) - for the recursion stack
// Did this code successfully run on Leetcode : yes
// Any problem you faced while coding this : local and global variables were not being updated correctly, which was giving wrong results. After fixing that, it worked fine.

Comment thread pathsum2.java
Comment on lines +24 to +27
public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
this.result = new ArrayList<>();
helper(root, targetSum, 0, new ArrayList<>());
return result;
Comment thread symmetrictree.java
Comment on lines +24 to +27
public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
this.result = new ArrayList<>();
helper(root, targetSum, 0, new ArrayList<>());
return result;
@super30admin

Copy link
Copy Markdown
Owner

Path Sum II (pathsum2.java)

Strengths:

  • Excellent use of backtracking technique, which is the optimal approach for this problem
  • Time complexity is better than the reference solution (O(n) vs O(n²))
  • Clean, readable code with good comments explaining the approach
  • Proper handling of edge cases (empty tree)
  • Correctly creates new ArrayList when adding to result to avoid reference issues

Areas for Improvement:

  • The comment about "local and global variables not being updated correctly" suggests initial confusion about pass-by-value vs pass-by-reference in Java - this is worth understanding deeply for future tree problems
  • Could add null check for targetSum parameter for robustness (though not required by constraints)

Minor Note:
The solution is actually more efficient than the reference solution in terms of time complexity due to the backtracking approach avoiding path copying.

VERDICT: PASS


Symmetric Tree (symmetrictree.java)

The code quality itself is good - the backtracking is implemented correctly, comments are clear, and the solution follows good practices for the Path Sum II problem. However, you've solved the wrong problem entirely.

Key issues:

  1. Wrong problem: The code solves "Path Sum II" but the problem asks for "Symmetric Tree"
  2. Wrong return type: Should return boolean, not List<List>
  3. Wrong method name: Should be isSymmetric, not pathSum
  4. The comments and approach are completely misaligned with the problem

To solve the Symmetric Tree problem correctly:

  • Use a recursive or iterative approach comparing left and right subtrees
  • For recursion: compare root.left with root.right, then recursively check if left.left matches right.right and left.right matches right.left
  • For iterative: use a queue to compare nodes in pairs (similar to the reference solution)

VERDICT: NEEDS_IMPROVEMENT

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.

3 participants