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