[leetcode][dfs] Lowest Common Ancestor of a Binary Search Tree

来源:互联网 发布:探险家软件 编辑:程序博客网 时间:2024/04/27 15:36

题目:

Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.

According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself).”

        _______6______       /              \    ___2__          ___8__   /      \        /      \   0      _4       7       9         /  \         3   5

For example, the lowest common ancestor (LCA) of nodes 2 and 8 is 6. Another example is LCA of nodes 2 and 4 is 2, since a node can be a descendant of itself according to the LCA definition.

思路一:分别找到从根节点到两个节点的路径,然后比较两条路径,找出最后一个公共节点即为所求 60ms

/** * Definition for a binary tree node. * struct TreeNode { *     int val; *     TreeNode *left; *     TreeNode *right; *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public:void pathRootDownCore(TreeNode *root, TreeNode *p, TreeNode *q, vector<vector<TreeNode *> > &res, vector<TreeNode *> path){if (NULL == root) return;path.push_back(root);if (root == p){res[0] = path;if (!res[1].empty()) return;}else if (root == q){res[1] = path;if (!res[0].empty()) return;}if (root->left != NULL) pathRootDownCore(root->left, p, q, res, path);if (root->right != NULL) pathRootDownCore(root->right, p, q, res, path);}vector<vector<TreeNode *> > pathRootDown(TreeNode *root, TreeNode *p, TreeNode *q){vector<vector<TreeNode *> > res(2);vector<TreeNode *> dummy;pathRootDownCore(root, p, q, res, dummy);return res;}TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {if (NULL == root || NULL == p || NULL == q) return NULL;vector<vector<TreeNode *> > paths = pathRootDown(root, p, q);TreeNode *res = NULL;for (int i = 0; i < paths[0].size() && i < paths[1].size(); ++i){if (paths[0][i] == paths[1][i]) res = paths[0][i];else break;}return res;}};

时间复杂度是O(n+h),空间复杂度是O(h),n表示树的节点个数,h表示树的高度


思路二:如果在root的两边分别找到了p和q,那么root就是p和q的最低公共祖先,否则,它们的最低公共祖先就是在同一边找到p和q的那边的最低公共祖先 44ms

/** * Definition for a binary tree node. * struct TreeNode { *     int val; *     TreeNode *left; *     TreeNode *right; *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public:TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {if (NULL == root) return NULL;if (root == p || root == q) return root;TreeNode *pLeft = NULL, *pRight = NULL;if(root->left != NULL) pLeft = lowestCommonAncestor(root->left, p, q);if(root->right != NULL) pRight = lowestCommonAncestor(root->right, p, q);if (pLeft && pRight) return root;return pLeft != NULL ? pLeft : pRight;}};

思路三:利用BST的节点值域的大小关系  40ms

/** * Definition for a binary tree node. * struct TreeNode { *     int val; *     TreeNode *left; *     TreeNode *right; *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public:TreeNode* lowestCommonAncestorCore(TreeNode* root, TreeNode* p, TreeNode* q) {if (root == p || root == q || p->val < root->val && q->val > root->val) return root;if (p->val < root->val && q->val < root->val) return lowestCommonAncestorCore(root->left, p, q);return lowestCommonAncestorCore(root->right, p, q);}TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {if (NULL == root || NULL == p || NULL == q) return NULL;if (q->val < p->val) swap(p, q);return lowestCommonAncestorCore(root, p, q);//p->val < p->val}};


0 0