1. /**
    2. * Definition for a binary tree node.
    3. * function TreeNode(val) {
    4. * this.val = val;
    5. * this.left = this.right = null;
    6. * }
    7. */
    8. /**
    9. * @param {TreeNode} root
    10. * @param {TreeNode} p
    11. * @param {TreeNode} q
    12. * @return {TreeNode}
    13. */
    14. var lowestCommonAncestor = function(root, p, q) {
    15. if(!root || root === p || root === q) return root
    16. let left = lowestCommonAncestor(root.left, p, q)
    17. let right = lowestCommonAncestor(root.right, p, q)
    18. if(left && right) {
    19. return root
    20. }
    21. return left ? left : right
    22. };