-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
772e739
commit 4e4a3ee
Showing
3 changed files
with
73 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/page-3/236. Lowest Common Ancestor of a Binary Tree/lowestCommonAncestor.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { TreeNode } from '~/utils/binary-tree'; | ||
|
||
import { lowestCommonAncestor } from './lowestCommonAncestor'; | ||
|
||
describe('236. Lowest Common Ancestor of a Binary Tree', () => { | ||
test('lowestCommonAncestor', () => { | ||
{ | ||
const node6 = new TreeNode(6); | ||
const node7 = new TreeNode(7); | ||
const node4 = new TreeNode(4); | ||
const node2 = new TreeNode(2, node7, node4); | ||
const node0 = new TreeNode(0); | ||
const node8 = new TreeNode(8); | ||
const node5 = new TreeNode(5, node6, node2); | ||
const node1 = new TreeNode(1, node0, node8); | ||
const root = new TreeNode(3, node5, node1); | ||
const p = node5; | ||
const q = node1; | ||
expect(lowestCommonAncestor(root, p, q)?.val).toBe(3); | ||
} | ||
|
||
{ | ||
const node6 = new TreeNode(6); | ||
const node7 = new TreeNode(7); | ||
const node4 = new TreeNode(4); | ||
const node2 = new TreeNode(2, node7, node4); | ||
const node0 = new TreeNode(0); | ||
const node8 = new TreeNode(8); | ||
const node5 = new TreeNode(5, node6, node2); | ||
const node1 = new TreeNode(1, node0, node8); | ||
const root = new TreeNode(3, node5, node1); | ||
const p = node5; | ||
const q = node4; | ||
expect(lowestCommonAncestor(root, p, q)?.val).toBe(5); | ||
} | ||
|
||
{ | ||
const node2 = new TreeNode(2); | ||
const root = new TreeNode(1, node2, null); | ||
const p = root; | ||
const q = node2; | ||
expect(lowestCommonAncestor(root, p, q)?.val).toBe(1); | ||
} | ||
}); | ||
}); |
26 changes: 26 additions & 0 deletions
26
src/page-3/236. Lowest Common Ancestor of a Binary Tree/lowestCommonAncestor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import type { TreeNode } from '~/utils/binary-tree'; | ||
|
||
type LowestCommonAncestor = ( | ||
root: TreeNode | null, | ||
p: TreeNode | null, | ||
q: TreeNode | null, | ||
) => TreeNode | null; | ||
|
||
/** | ||
* Accepted | ||
*/ | ||
export const lowestCommonAncestor: LowestCommonAncestor = (root, p, q) => { | ||
if (root === null || root === p || root === q) return root; | ||
|
||
// Perform DFS on the left subtree | ||
const left = lowestCommonAncestor(root.left, p, q); | ||
|
||
// Perform DFS on the right subtree | ||
const right = lowestCommonAncestor(root.right, p, q); | ||
|
||
// If both left and right are non-null, the current node is the LCA | ||
if (left !== null && right !== null) return root; | ||
|
||
// If only one of the sides is non-null, return that side | ||
return left !== null ? left : right; | ||
}; |