recursive c++ solution


  • 1
    H
    class Solution {
    public:
        TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
            if(!root || root == p || root == q) return root;
            TreeNode* parent1 = lowestCommonAncestor(root->left, p, q);
            TreeNode* parent2 = lowestCommonAncestor(root->right, p, q);
            if(parent1 && parent2) return root;
            return parent1 ? parent1 : parent2;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.