/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ class Solution { public: /** * * @param root TreeNode类 * @param o1 int整型 * @param o2 int整型 * @return int整型 */ int lowestCommonAncestor(TreeNode* root, int o1, int o2) { // write code here return findh(root, o1, o2)->val; } TreeNode* findh(TreeNode* root, int o1, int o2) { if(root == NULL) return NULL; if (root->val == o1 || root->val==o2) return root; TreeNode* ifLeftHave = findh(root->left, o1, o2); TreeNode* ifRightHave = findh(root->right, o1, o2); if(ifRightHave !=NULL && ifLeftHave != NULL) return root; else if (ifLeftHave != NULL) return ifLeftHave; else return ifRightHave; } };