1+ import { describe , test , expect } from 'bun:test'
2+ import { lowestCommonAncestor , TreeNode } from './lowest-common-ansertor' ;
3+
4+ describe ( 'lowestCommonAncestor' , ( ) => {
5+ test ( 'should return null for an empty tree' , ( ) => {
6+ expect ( lowestCommonAncestor ( null , null , null ) ) . toBeNull ( ) ;
7+ } ) ;
8+
9+ test ( 'should return the root for the same node' , ( ) => {
10+ const root = new TreeNode ( 1 ) ;
11+ expect ( lowestCommonAncestor ( root , root , root ) ) . toBe ( root ) ;
12+ } ) ;
13+
14+ test ( 'should return the root as LCA when one node is the root' , ( ) => {
15+ const root = new TreeNode ( 1 , new TreeNode ( 2 ) , new TreeNode ( 3 ) ) ;
16+ expect ( lowestCommonAncestor ( root , root , root . left ) ) . toBe ( root ) ;
17+ expect ( lowestCommonAncestor ( root , root , root . right ) ) . toBe ( root ) ;
18+ } ) ;
19+
20+ test ( 'should return the correct LCA for two nodes on the same level' , ( ) => {
21+ const root = new TreeNode ( 3 , new TreeNode ( 2 ) , new TreeNode ( 4 ) ) ;
22+ expect ( lowestCommonAncestor ( root , root . left , root . right ) ) . toBe ( root ) ;
23+ } ) ;
24+
25+ test ( 'should return the correct LCA for two nodes in the left subtree' , ( ) => {
26+ const root = new TreeNode ( 5 ,
27+ new TreeNode ( 3 , new TreeNode ( 2 ) , new TreeNode ( 4 ) ) ,
28+ new TreeNode ( 8 )
29+ ) ;
30+ if ( root . left && root . left . left && root . left . right ) {
31+ expect ( lowestCommonAncestor ( root , root . left . left , root . left . right ) ) . toBe ( root . left ) ;
32+ }
33+ } ) ;
34+
35+ test ( 'should return the correct LCA for two nodes in the right subtree' , ( ) => {
36+ const root = new TreeNode ( 5 ,
37+ new TreeNode ( 3 ) ,
38+ new TreeNode ( 8 , new TreeNode ( 7 ) , new TreeNode ( 9 ) )
39+ ) ;
40+ if ( root . right && root . right . left && root . right . right ) {
41+ expect ( lowestCommonAncestor ( root , root . right . left , root . right . right ) ) . toBe ( root . right ) ;
42+ }
43+ } ) ;
44+
45+ test ( 'should return the correct LCA for nodes in different subtrees' , ( ) => {
46+ const root = new TreeNode ( 6 ,
47+ new TreeNode ( 2 , new TreeNode ( 0 ) , new TreeNode ( 4 , new TreeNode ( 3 ) , new TreeNode ( 5 ) ) ) ,
48+ new TreeNode ( 8 , new TreeNode ( 7 ) , new TreeNode ( 9 ) )
49+ ) ;
50+ if ( root . left && root . right ) {
51+ expect ( lowestCommonAncestor ( root , root . left , root . right ) ) . toBe ( root ) ;
52+ if ( root . left . left ) {
53+ expect ( lowestCommonAncestor ( root , root . left . left , root . right ) ) . toBe ( root ) ;
54+ }
55+ }
56+ } ) ;
57+
58+ test ( 'should return the correct LCA for nodes that are not direct children' , ( ) => {
59+ const root = new TreeNode ( 6 ,
60+ new TreeNode ( 2 , new TreeNode ( 0 ) , new TreeNode ( 4 , new TreeNode ( 3 ) , new TreeNode ( 5 ) ) ) ,
61+ new TreeNode ( 8 , new TreeNode ( 7 ) , new TreeNode ( 9 ) )
62+ ) ;
63+ if ( root . left && root . left . right && root . left . right . left && root . left . right . right ) {
64+ expect ( lowestCommonAncestor ( root , root . left . right . left , root . left . right . right ) ) . toBe ( root . left . right ) ;
65+ }
66+ } ) ;
67+ } ) ;
0 commit comments