/** * @param root: The root of the binary search tree. * @param A and B: two nodes in a Binary. * @return: Return the least common ancestor(LCA) of the two nodes. */ TreeNode *lowestCommonAncestor(TreeNode *root, TreeNode *A, TreeNode *B) { // write your code here if(root == NULL || A == NULL || B == NULL) { return NULL; }//if vector<TreeNode *> lA,lB,cur; dfs(cur,root,A,B,lA,lB); return getLastCommonNode(lA,lB); }
TreeNode* getLastCommonParent( TreeNode* root, TreeNode* node1, TreeNode* node2 ) { if (root == NULL || node1 == NULL || node2 == NULL) { return NULL; } list<TreeNode*> path1; getNodePath(root, node1, path1); list<TreeNode*> path2; getNodePath(root, node2, path2); return getLastCommonNode(path1, path2); }