题解 | #在二叉树中找到两个节点的最近公共祖先#
在二叉树中找到两个节点的最近公共祖先
https://www.nowcoder.com/practice/e0cc33a83afe4530bcec46eba3325116
struct TreeNode* low(struct TreeNode* root, int o1, int o2) { if (root == NULL || root->val == o1 || root->val == o2) { return root; } struct TreeNode* left = low(root->left, o1, o2); struct TreeNode* right = low(root->right, o1, o2); if (left != NULL && right != NULL) { return root; } return left != NULL ? left : right; } int lowestCommonAncestor(struct TreeNode* root, int o1, int o2 ) { return low(root,o1,o2)->val; }