235.236. Lowest Common Ancestor of a Binary (Search) Tree--Recent public ancestor

Source: Internet
Author: User

235. Lowest Common Ancestor of a Binary Search Tree

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

According to the definition of the LCA in Wikipedia: "The lowest common ancestor is defined between," nodes V and W as the L Owest node in T, have 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 and are 2 8 6 . Another example is LCA of nodes 2 4 2 and are, since a node can be a descendant of itself according to the L CA definition.

Treenode* lowestcommonancestor (treenode* root, treenode* p, treenode*q) { while(root) {if(P->val > Root->val && q->val > root->val) {Root= root->Right ; Continue; }        if(P->val < Root->val && Q->val < root->val) {Root= root->Left ; Continue; }        returnRoot; }    returnNULL;}

236. Lowest Common Ancestor of a Binary Tree

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

According to the definition of the LCA in Wikipedia: "The lowest common ancestor is defined between," nodes V and W as the L Owest node in T, have both V and W as descendants (where we allow a node to be a descendant of itself). "

        _______3______       /                  ___5__          ___1__   /      \        /         6      _2       0       8         /           7   4

For example, the lowest common ancestor (LCA) of nodes and are 5 1 3 . Another example is LCA of nodes 5 4 5 and are, since a node can be a descendant of itself according to the L CA definition.

(1) Find two nodes at a time recursively

Treenode* LowestCommonAncestor02 (treenode* root, treenode* p, treenode*q) {//return if found or not found, return NULL if not found    if(Root==null | | root = = P | | root = = q)returnRoot; //find the LCA in left treeTreenode* left = LowestCommonAncestor02 (root->Left , p, q); //find the LCA in right treetreenode* right = LowestCommonAncestor02 (root->Right , p, q); //left==null means both ' P ' and ' Q ' is not found in the left tree.    if(Left==null)returnRight ; //right==null means both ' P ' and ' Q ' is not found on right tree.    if(Right==null)returnLeft ; //Left!=null && Right!=null, which means ' P ' & ' Q ' is seperated in left and right tree.    returnRoot;}

(2) record the root node to the node of the first sequence traversal path, return the last same node in the path

BOOLFindpath (treenode* root, treenode* p, vector<treenode*>&path) {    if(Root==null)return false; if(Root = =p) {path.push_back (P); return true;    } path.push_back (root); if(Findpath (Root->left, p, path))return true; if(Findpath (Root->right, p, path))return true;            Path.pop_back (); return false;}//Ordinary, find the path and comapre the path.Treenode* LowestCommonAncestor01 (treenode* root, treenode* p, treenode*q) {vector<TreeNode*>path1, path2; if(!findpath (Root, P, path1))returnNULL; if(!findpath (Root, Q, path2))returnNULL; intLen = Path1.size () < Path2.size ()?path1.size (): Path2.size (); TreeNode* result =Root;  for(intI=0; i<len; i++) {        if(Path1[i]! =Path2[i]) {            returnresult; } result=Path1[i]; }    returnresult;}

235.236. Lowest Common Ancestor of a Binary (Search) Tree--Recent public ancestor

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.