|
| 1 | +#include <iostream> |
| 2 | +#include <vector> |
| 3 | +#include <string> |
| 4 | +#include <algorithm> |
| 5 | +#include <cstdio> |
| 6 | +#include <list> |
| 7 | +using namespace std; |
| 8 | +struct TreeNode { |
| 9 | + int val; |
| 10 | + TreeNode *left; |
| 11 | + TreeNode *right; |
| 12 | + TreeNode(int x) : val(x), left(nullptr), right(nullptr){} |
| 13 | +}; |
| 14 | +class Solution { |
| 15 | +public: |
| 16 | + TreeNode *lowestCommonAncestor(TreeNode *root, TreeNode *p, TreeNode *q) |
| 17 | + { |
| 18 | + if (root == nullptr) |
| 19 | + return nullptr; |
| 20 | + list<TreeNode *> l1, l2; |
| 21 | + getPath(root, p, l1); |
| 22 | + getPath(root, q, l2); |
| 23 | + /* |
| 24 | + for_each(begin(l1), end(l1), [](TreeNode *t){cout << t->val << ' ';}); |
| 25 | + cout << endl; |
| 26 | + for_each(begin(l2), end(l2), [](TreeNode *t){cout << t->val << ' ';}); |
| 27 | + cout << endl; |
| 28 | + */ |
| 29 | + return findCommonNode(l1, l2); |
| 30 | + } |
| 31 | + /* |
| 32 | + TreeNode *lowestCommonAncestor(TreeNode *root, TreeNode *p, TreeNode *q) |
| 33 | + { |
| 34 | + if (root == nullptr) |
| 35 | + return nullptr; |
| 36 | + if (root == p || root == q) |
| 37 | + return root; |
| 38 | + TreeNode *left = lowestCommonAncestor(root->left, p, q); |
| 39 | + TreeNode *right = lowestCommonAncestor(root->right, p, q); |
| 40 | + if (left && right) |
| 41 | + return root; |
| 42 | + return left ? left:right; |
| 43 | + } |
| 44 | + */ |
| 45 | +private: |
| 46 | + bool getPath(TreeNode *root, TreeNode *p, list<TreeNode *> &path) { |
| 47 | + if (root == nullptr) |
| 48 | + return p == nullptr; |
| 49 | + path.push_back(root); |
| 50 | + if (root == p) |
| 51 | + return true; |
| 52 | + if (getPath(root->left, p, path) || getPath(root->right, p, path)) |
| 53 | + return true; |
| 54 | + else { |
| 55 | + path.pop_back(); |
| 56 | + return false; |
| 57 | + } |
| 58 | + } |
| 59 | + TreeNode *findCommonNode(const list<TreeNode *> &l1, const list<TreeNode *> &l2) { |
| 60 | + int n = l1.size(); |
| 61 | + int m = l2.size(); |
| 62 | + auto i = begin(l1), j = begin(l2); |
| 63 | + TreeNode *last = nullptr; |
| 64 | + while (i != end(l1) && j != end(l2)) { |
| 65 | + if (*i == *j) { |
| 66 | + last = *i; |
| 67 | + } |
| 68 | + i++, j++; |
| 69 | + } |
| 70 | + return last; |
| 71 | + } |
| 72 | +}; |
| 73 | +TreeNode *mk_node(int val) |
| 74 | +{ |
| 75 | + return new TreeNode(val); |
| 76 | +} |
| 77 | +TreeNode *mk_child(TreeNode *root, TreeNode *left, TreeNode *right) |
| 78 | +{ |
| 79 | + root->left = left; |
| 80 | + root->right = right; |
| 81 | + return root; |
| 82 | +} |
| 83 | +TreeNode *mk_child(TreeNode *root, int left, int right) |
| 84 | +{ |
| 85 | + return mk_child(root, new TreeNode(left), new TreeNode(right)); |
| 86 | +} |
| 87 | +TreeNode *mk_child(int root, int left, int right) |
| 88 | +{ |
| 89 | + return mk_child(new TreeNode(root), new TreeNode(left), new TreeNode(right)); |
| 90 | +} |
| 91 | +int main(int argc, char **argv) |
| 92 | +{ |
| 93 | + Solution solution; |
| 94 | + TreeNode *root = mk_child(3, 5, 1); |
| 95 | + mk_child(root->left, 6, 2); |
| 96 | + mk_child(root->left->right, 7, 4); |
| 97 | + mk_child(root->right, 0, 8); |
| 98 | + cout << solution.lowestCommonAncestor(root, root->left, root->right)->val << endl; |
| 99 | + cout << solution.lowestCommonAncestor(root, root->left, root->left->right->right)->val << endl; |
| 100 | + return 0; |
| 101 | +} |
0 commit comments