class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if (root == null || root == p || root == q) return root;
TreeNode left_l = lowestCommonAncestor(root.left,p,q);
TreeNode right_r = lowestCommonAncestor(root.right,p,q);
if (left_l == null && right_r == null) return null;
else if (left_l != null && right_r == null) return left_l;//返回最先找到的那个结点
else if (left_l == null && right_r != null) return right_r;
else return root;
}
}
不要人脑压栈 对于处理不同的情况用最简单的例子进行解释,比如题目中说的如果 p == root的话 那单层循环就是 两个结点来模拟



