public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q){
if(root == null) return root;
if(root == p || root = q) return root;
//按照下面的图,认真将递归的过程在脑子中走一遍,可以帮助加深理解。
TreeNode left = lowestCommonAncestor(root.left, p, q);
TreeNode right = lowestCommonAncestor(root.right, p, q);
if(left != null && right != null){ // p, q 分属在左右两个子树上
return root;
} else if(left != null){
return left;
} else if(right != null){
return right;
}
return null;
}