image.png
    image.png
    解析:

    1. class Solution {
    2. public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
    3. if(root == null) return null;
    4. if(root==p || root==q) return root;
    5. TreeNode left = lowestCommonAncestor(root.left, p, q);
    6. TreeNode right = lowestCommonAncestor(root.right, p, q);
    7. if (left!=null && right!=null) return root;
    8. if (left==null) return right;
    9. return left;
    10. }
    11. }