File tree Expand file tree Collapse file tree 1 file changed +9
-3
lines changed 
src/main/java/g0201_0300/s0236_lowest_common_ancestor_of_a_binary_tree Expand file tree Collapse file tree 1 file changed +9
-3
lines changed Original file line number Diff line number Diff line change 22
33// #Medium #Top_100_Liked_Questions #Depth_First_Search #Tree #Binary_Tree 
44// #Data_Structure_II_Day_18_Tree #Udemy_Tree_Stack_Queue #Big_O_Time_O(n)_Space_O(n) 
5- // #2024_11_16_Time_6_ms_(100.00%)_Space_44.8_MB_(36.66 %) 
5+ // #2024_11_16_Time_6_ms_(100.00%)_Space_44_MB_(98.99 %) 
66
77import  com_github_leetcode .TreeNode ;
88
1717 */ 
1818public  class  Solution  {
1919    public  TreeNode  lowestCommonAncestor (TreeNode  root , TreeNode  p , TreeNode  q ) {
20-         if  (root  == null  || root  == p  || root  == q ) {
20+         if  (root  == null ) {
21+             return  null ;
22+         }
23+         if  (root .val  == p .val  || root .val  == q .val ) {
2124            return  root ;
2225        }
2326        TreeNode  left  = lowestCommonAncestor (root .left , p , q );
2427        TreeNode  right  = lowestCommonAncestor (root .right , p , q );
2528        if  (left  != null  && right  != null ) {
2629            return  root ;
2730        }
28-         return  left  != null  ? left  : right ;
31+         if  (left  != null ) {
32+             return  left ;
33+         }
34+         return  right ;
2935    }
3036}
 
 
   
 
     
   
   
          
    
    
     
    
      
     
     
    You can’t perform that action at this time.
  
 
    
  
    
      
        
     
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments