Skip to content

Commit

Permalink
feat: same-tree solution
Browse files Browse the repository at this point in the history
  • Loading branch information
YeomChaeeun committed Feb 26, 2025
1 parent cf5691a commit e7efcea
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions same-tree/YeomChaeeun.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/
/**
* 같은 트리인지 확인하기
* 알고리즘 복잡도
* - 시간 복잡도: O(n)
* - 공간 복잡도: O(n)
* @param p
* @param q
*/
function isSameTree(p: TreeNode | null, q: TreeNode | null): boolean {
if(!p || !q) {
return p === q;
}

return p.val === q.val && isSameTree(p.left, q.left) && isSameTree(p.right, q.right)

}

0 comments on commit e7efcea

Please sign in to comment.