1. // 1)O1是O2的LCA,或O2是O1的LCA。
    2. // 2)O1与O2不互为LCA,向上汇聚找到。
    3. public static Node LowestAncestor(Node head , Node o1 , Node o2){
    4. if( head == null || head == o1 || head == o2){ // basecase
    5. return head;
    6. }
    7. Node leftNode = LowestAncestor(head.left, o1, o2);
    8. Node rightNode = LowestAncestor(head.right, o1, o2);
    9. // 左右两边都不返回为空 ,肯定是一个公共祖先,返回自己
    10. if(leftNode != null && rightNode != null ){
    11. return head;
    12. }
    13. // 左右两棵树,并不是都有返回值
    14. return leftNode !=null ? leftNode : rightNode;
    15. }