Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions PathSum2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import java.util.*;
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
// SC = O(h)
// TC = O(nh)
/*
This solution uses DFS with backtracking to find all root-to-leaf paths whose sum equals the target.
Starting from the root, each visited node is added to the current path (list), and its value is subtracted from targetSum.
If a leaf node is reached and the remaining targetSum is 0, the current path is a valid answer and is added to the result.
The recursion continues by exploring both the left and right subtrees.
After exploring a node's children, the node is removed from the current path (backtracking) so the same list can be reused for other paths.
*/
List<List<Integer>> result = new ArrayList<>();
public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
dfs(root, targetSum, new ArrayList<Integer>());
return result;
}

private void dfs(TreeNode root, int targetSum, List<Integer> list) {
if (root == null) {
return;
}

targetSum -= root.val;
list.add(root.val);

if (targetSum == 0 && root.left == null && root.right == null) {
result.add(new ArrayList<>(list));
}

dfs(root.left, targetSum, list);
dfs(root.right, targetSum, list);
list.remove(list.size() - 1);
}
}
49 changes: 49 additions & 0 deletions SymmetricTree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import java.util.*;

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
/*
TC - O(n)
SC - O(n)
We use BFS for this solution (level-order traversal with a queue).

We start by adding the root twice into a queue to compare the tree with itself in a mirrored way.
In each step, two nodes (t1 and t2) are taken from the queue and compared:
If both are null, continue (they match). If only one is null, or their values are different, the tree is not symmetric.
If values match, their children are added and compared in the following order :-
t1.left with t2.right
t1.right with t2.left
This ensures we always compare opposite sides of the tree. If all pairs match, the tree is symmetric.
*/
public boolean isSymmetric(TreeNode root) {
Queue<TreeNode> q = new LinkedList<>();
q.add(root);
q.add(root);
while (!q.isEmpty()) {
TreeNode t1 = q.poll();
TreeNode t2 = q.poll();
if (t1 == null && t2 == null) continue;
if (t1 == null || t2 == null) return false;
if (t1.val != t2.val) return false;
q.add(t1.left);
q.add(t2.right);
q.add(t1.right);
q.add(t2.left);
}
return true;
}
}