Skip to content

Commit

Permalink
Add same-tree solution
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeehay28 committed Feb 24, 2025
1 parent 7104cf7 commit eb55547
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions same-tree/Jeehay28.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// πŸš€ Iterative DFS (Stack)
// βœ… Time Complexity: O(n), where n is the number of nodes in the tree
// βœ… Space Complexity: O(n) (worst case), O(log n) (best case for balanced trees)

/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} p
* @param {TreeNode} q
* @return {boolean}
*/
var isSameTree = function (p, q) {
let stack = [[p, q]];
while (stack.length > 0) {
const [p, q] = stack.pop();

if (p === null && q === null) continue;

if (p === null || q === null) return false;

if (p.val !== q.val) return false;

stack.push([p.left, q.left]);
stack.push([p.right, q.right]);
}

return true;
};



// πŸš€ recursive approach
// βœ… Time Complexity: O(n), where n is the number of nodes in the tree
// βœ… Space Complexity: O(n) (worst case), O(log n) (best case for balanced trees)

/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} p
* @param {TreeNode} q
* @return {boolean}
*/
// var isSameTree = function (p, q) {
// // Base case: If both trees are empty, they are the same
// if (p === null && q === null) return true;

// // If one of the trees is empty and the other is not, return false
// if (p === null || q === null) return false;

// // Compare the values of the current nodes
// if (p.val !== q.val) return false;

// // Recursively compare the left and right subtrees
// return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
// };


0 comments on commit eb55547

Please sign in to comment.