|
| 1 | +// Source : https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ |
| 2 | +// Author : Hao Chen |
| 3 | +// Date : 2015-07-17 |
| 4 | + |
| 5 | +/********************************************************************************** |
| 6 | + * |
| 7 | + * Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the |
| 8 | + * tree. |
| 9 | + * |
| 10 | + * According to the definition of LCA on Wikipedia: “The lowest common ancestor is |
| 11 | + * defined between two nodes v and w as the lowest node in T that has both v and w as |
| 12 | + * descendants (where we allow a node to be a descendant of itself).” |
| 13 | + * |
| 14 | + * _______3______ |
| 15 | + * / \ |
| 16 | + * ___5__ ___1__ |
| 17 | + * / \ / \ |
| 18 | + * 6 _2 0 8 |
| 19 | + * / \ |
| 20 | + * 7 4 |
| 21 | + * |
| 22 | + * For example, the lowest common ancestor (LCA) of nodes 5 and 1 is 3. Another example |
| 23 | + * is LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according |
| 24 | + * to the LCA definition. |
| 25 | + * |
| 26 | + * |
| 27 | + * |
| 28 | + **********************************************************************************/ |
| 29 | + |
| 30 | + |
| 31 | +/** |
| 32 | + * Definition for a binary tree node. |
| 33 | + * struct TreeNode { |
| 34 | + * int val; |
| 35 | + * TreeNode *left; |
| 36 | + * TreeNode *right; |
| 37 | + * TreeNode(int x) : val(x), left(NULL), right(NULL) {} |
| 38 | + * }; |
| 39 | + */ |
| 40 | +class Solution { |
| 41 | +public: |
| 42 | + bool findPath(TreeNode* root, TreeNode* p, vector<TreeNode*>& path) { |
| 43 | + if (root==NULL) return false; |
| 44 | + if (root == p) { |
| 45 | + path.push_back(p); |
| 46 | + return true; |
| 47 | + } |
| 48 | + |
| 49 | + path.push_back(root); |
| 50 | + if (findPath(root->left, p, path)) return true; |
| 51 | + if (findPath(root->right, p, path)) return true; |
| 52 | + path.pop_back(); |
| 53 | + |
| 54 | + return false; |
| 55 | + } |
| 56 | + |
| 57 | + //Ordinary way, find the path and comapre the path. |
| 58 | + TreeNode* lowestCommonAncestor01(TreeNode* root, TreeNode* p, TreeNode* q) { |
| 59 | + |
| 60 | + vector<TreeNode*> path1, path2; |
| 61 | + |
| 62 | + if (!findPath(root, p, path1)) return NULL; |
| 63 | + if (!findPath(root, q, path2)) return NULL; |
| 64 | + |
| 65 | + int len = path1.size() < path2.size() ? path1.size() : path2.size(); |
| 66 | + TreeNode* result = root; |
| 67 | + for(int i=0; i<len; i++) { |
| 68 | + if (path1[i] != path2[i]) { |
| 69 | + return result; |
| 70 | + } |
| 71 | + result = path1[i]; |
| 72 | + } |
| 73 | + return result; |
| 74 | + } |
| 75 | + |
| 76 | + //Actually, we can do the recursive search in one time |
| 77 | + TreeNode* lowestCommonAncestor02(TreeNode* root, TreeNode* p, TreeNode* q) { |
| 78 | + |
| 79 | + //return if found or not found, return NULL if not found |
| 80 | + if (root==NULL || root == p || root == q) return root; |
| 81 | + |
| 82 | + //find the LCA in left tree |
| 83 | + TreeNode* left = lowestCommonAncestor02(root->left, p, q); |
| 84 | + //find the LCA in right tree |
| 85 | + TreeNode* right = lowestCommonAncestor02(root->right, p, q); |
| 86 | + |
| 87 | + //left==NULL means both `p` and `q` are not found in left tree. |
| 88 | + if (left==NULL) return right; |
| 89 | + //right==NULL means both `p` and `q` are not found in right tree. |
| 90 | + if (right==NULL) return left; |
| 91 | + // left!=NULL && right !=NULL, which means `p` & `q` are seperated in left and right tree. |
| 92 | + return root; |
| 93 | + } |
| 94 | + |
| 95 | + TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { |
| 96 | + srand(time(0)); |
| 97 | + if (random()%2) { |
| 98 | + return lowestCommonAncestor02(root, p, q); |
| 99 | + } |
| 100 | + return lowestCommonAncestor01(root, p, q); |
| 101 | + } |
| 102 | +}; |
0 commit comments