import java.util.*;

/*

  • public class TreeNode {

  • int val = 0;

  • TreeNode left = null;

  • TreeNode right = null;

  • public TreeNode(int val) {

  • this.val = val;
    
  • }

  • } / //和上一道题一样啊 public class Solution { /*

    • 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
    • @param root TreeNode类
    • @param p int整型
    • @param q int整型
    • @return int整型 */ public int lowestCommonAncestor (TreeNode root, int p, int q) { return isEqual(root, p, q).val; }

    public TreeNode isEqual(TreeNode root,int o1,int o2) { if(root == null) return null;

     if(root.val==o1 || root.val==o2) return root;
     
     TreeNode left = isEqual(root.left, o1, o2);
     TreeNode right = isEqual(root.right, o1, o2);
     
     if(left!=null && right!=null) return root;
     if(left!=null && right==null) return left;
     if(right!=null && left==null) return right;
     
     return null;
    

    } }