Advertisement
Marvin48

236. Lowest Common Ancestor of a Binary Tree

Jan 18th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.06 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <stack>
  4. #include "TreeHelper.hpp"
  5.  
  6. using namespace std;
  7.  
  8. //        _______3______
  9. //       /              \
  10. //    ___5__          ___1__
  11. //   /      \        /      \
  12. //  6       2       0       8
  13. // /  \
  14. //7   4
  15.  
  16.  
  17. //       37
  18. //    /      \
  19. // -34       -48
  20. //     \    /    \
  21. //  -100  -100    48
  22. //                /
  23. //              -54
  24. //             /   \
  25. //           -71    -22
  26. //                    \
  27. //                     8
  28.  
  29. class Solution {
  30. private:
  31.     bool p_found, q_found;
  32.    
  33. public:
  34.     Solution() {
  35.         p_found = false;
  36.         q_found = false;
  37.     }
  38.    
  39.     TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
  40.         if (root == nullptr)
  41.             return nullptr;
  42. //        printf("node: %d\n", root->val);
  43.        
  44.         if (!p_found && root->val == p->val)
  45.             p_found = true;
  46.         if (!q_found && root->val == q->val)
  47.             q_found = true;
  48.         bool current_node_match = root->val == p->val || root->val == q->val;
  49.        
  50.         if (p_found && q_found && current_node_match)
  51.             return root;
  52.        
  53.         //look in left sub-tree
  54.         TreeNode *ret1 = lowestCommonAncestor(root->left, p, q);
  55.         if (p_found && q_found)
  56.             return current_node_match ? root : ret1;
  57.        
  58.         //look in right sub tree
  59.         TreeNode *ret2 = lowestCommonAncestor(root->right, p, q);
  60.         if (p_found && q_found)
  61.             return current_node_match ? root : (ret1 && ret2 ? root : ret2);
  62.        
  63.         if (current_node_match)
  64.             return root;
  65.         return ret1 ? ret1 : ret2;
  66.     }
  67. };
  68.  
  69. class Tester {
  70. public:
  71.     static int test_num;
  72.  
  73.     template <class P1Type, class P2Type, class P3Type, class ExpType>
  74.     void check(P1Type p1, P2Type p2, P3Type p3, ExpType expected) {
  75.         Solution sol;
  76.         test_num++;
  77.         P1Type res = sol.lowestCommonAncestor(p1, p2, p3);
  78.         if (res->val != expected) {
  79.             cout << "test " << test_num << ": Failed: got " << res->val << endl;
  80.         }
  81.         else {
  82.             printf("test %d: Passed\n", test_num);
  83.         }
  84.     }
  85. };
  86.  
  87. int Tester::test_num = 0;
  88.  
  89.  
  90. int main(int argc, const char * argv[]) {
  91.     Solution sol;
  92.     Tester t;
  93.    
  94.     string tree1 = "3,5,1,6,2,0,8,7,4,#,#,#,#,#,#,#,#,#,#";
  95.     TreeNode *root = TreeHelper::deserialize<TreeNode>(tree1);
  96.    
  97.     t.check<TreeNode*, TreeNode*, TreeNode*, int>(root, root->left->left, root->left, 5);
  98.     t.check<TreeNode*, TreeNode*, TreeNode*, int>(root, root->right->right, root->right, 1);
  99.     t.check<TreeNode*, TreeNode*, TreeNode*, int>(root, root->left, root->right, 3);
  100.     t.check<TreeNode*, TreeNode*, TreeNode*, int>(root, root->left->left->left, root->left->right, 5);
  101.    
  102.     string tree2 = "37,-34,-48,#,-100,-100,48,#,#,#,#,-54,#,-71,-22,#,#,#,8";
  103.     root = TreeHelper::deserialize<TreeNode>(tree2);
  104.     t.check<TreeNode*, TreeNode*, TreeNode*, int>(root, root->right->left, root->right->right->left->left, -48);
  105.    
  106.     return 0;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement