From 4e4a3ee101c2f5d780ce607cd30354e2924d808b Mon Sep 17 00:00:00 2001 From: Shyam-Chen Date: Sun, 25 Aug 2024 22:57:23 +0800 Subject: [PATCH] 197th Commit --- README.md | 3 +- .../lowestCommonAncestor.test.ts | 45 +++++++++++++++++++ .../lowestCommonAncestor.ts | 26 +++++++++++ 3 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 src/page-3/236. Lowest Common Ancestor of a Binary Tree/lowestCommonAncestor.test.ts create mode 100644 src/page-3/236. Lowest Common Ancestor of a Binary Tree/lowestCommonAncestor.ts diff --git a/README.md b/README.md index 0785686..a024763 100644 --- a/README.md +++ b/README.md @@ -176,13 +176,14 @@ Ace Coding Interview with 75 Qs | 1448. Count Good Nodes in Binary Tree | [Solution][1448] | Medium | | 437. Path Sum III | [Solution][437] | Medium | | 1372. Longest ZigZag Path in a Binary Tree | [Solution][1372] | Medium | -| 236. Lowest Common Ancestor of a Binary Tree | Solution | Medium | +| 236. Lowest Common Ancestor of a Binary Tree | [Solution][236] | Medium | [104]: ./src/page-2/104.%20Maximum%20Depth%20of%20Binary%20Tree/maxDepth.ts [872]: ./src/page-9/872.%20Leaf-Similar%20Trees/leafSimilar.ts [1448]: ./src/page-14/1448.%20Count%20Good%20Nodes%20in%20Binary%20Tree/goodNodes.ts [437]: ./src/page-5/437.%20Path%20Sum%20III/pathSum.ts [1372]: ./src/page-13/1372.%20Longest%20ZigZag%20Path%20in%20a%20Binary%20Tree/longestZigZag.ts +[236]: ./src/page-3/236.%20Lowest%20Common%20Ancestor%20of%20a%20Binary%20Tree/lowestCommonAncestor.ts | Binary Tree - BFS | | | | ---------------------------------------- | -------- | ------ | diff --git a/src/page-3/236. Lowest Common Ancestor of a Binary Tree/lowestCommonAncestor.test.ts b/src/page-3/236. Lowest Common Ancestor of a Binary Tree/lowestCommonAncestor.test.ts new file mode 100644 index 0000000..eb2d377 --- /dev/null +++ b/src/page-3/236. Lowest Common Ancestor of a Binary Tree/lowestCommonAncestor.test.ts @@ -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); + } + }); +}); diff --git a/src/page-3/236. Lowest Common Ancestor of a Binary Tree/lowestCommonAncestor.ts b/src/page-3/236. Lowest Common Ancestor of a Binary Tree/lowestCommonAncestor.ts new file mode 100644 index 0000000..6e9df9e --- /dev/null +++ b/src/page-3/236. Lowest Common Ancestor of a Binary Tree/lowestCommonAncestor.ts @@ -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; +};