应该说明查找的两个节点不能重复吧。很容易引起歧义。
class Solution { public: int lowestCommonAncestor(TreeNode* root, int o1, int o2) { return dfs(root, o1, o2)->val; } TreeNode* dfs(TreeNode* root, const int& p, const int& q){ if(!root || root->val == p || root->val == q) return root; TreeNode* ls = dfs(root->left, p, q); TreeNode* rs = dfs(root->right, p, q); if(ls == NULL) return rs; // 左右节点祖先在右边 if(rs == NULL) return ls; // 左右节点祖先在左边 return root; } };