Skip to content

Commit

Permalink
197th Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Chen committed Aug 25, 2024
1 parent 772e739 commit 4e4a3ee
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 | | |
| ---------------------------------------- | -------- | ------ |
Expand Down
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);
}
});
});
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;
};

0 comments on commit 4e4a3ee

Please sign in to comment.