[Leetcode] Lowest Common Ancestor of a binary search tree two forks the smallest common parent node

Source: Internet
Author: User

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.

This question asks us to find the smallest common parent node of the binary search tree, and the question about BST in Leetcode has validate binary search tree, which is Recover binary searching trees, binary search tree RCH Tree Iterator Two-fork search trees iterator, unique binary search Trees-only two-fork searching tree, unique binary search Trees II unique two-fork search Tree II, Convert Sort The ED array to binary search tree converts an ordered array to a binary-Sorted, and converts the list to binary searches tree to convert the ordered list to a binary search and Kth smallest elemen T in a BST two forks the small element of the K in the search tree. This problem can be solved by recursion, we first look at the example given in the topic, because the binary search tree is characterized by left < root < Right, so the value of the root node is always the middle value, greater than the left subtree of all node values, less than the right subtree of all the node values, then we can do the following judgment, If the value of the root node is greater than the large value between P and Q, note that P and q are all in the left subtree, then we go into the left child node of the root node to continue recursion, if the root node is less than P and q between the smaller values, the P and Q are in the right subtree, then we go into the root node of the right child node continue to recursion, if not, It means that the current root node is the smallest common parent node and can be returned directly, see the code below:

Solution One:

classSolution { Public: TreeNode* Lowestcommonancestor (treenode* root, treenode* p, treenode*q) {if(!root)returnNULL; if(Root->val > Max (p->val, q->val)) returnLowestcommonancestor (root->Left , p, q); Else if(Root->val < min (P->val, q->val)) returnLowestcommonancestor (root->Right , p, q); Else returnRoot; }};

Of course, this problem also has a non-recursive way of writing, use a while loop instead of recursive calls, and then constantly update the current root node, but also to achieve the same effect, the code is as follows:

Solution Two:

classSolution { Public: TreeNode* Lowestcommonancestor (treenode* root, treenode* p, treenode*q) { while(true) {            if(Root->val > Max (p->val, q->val)) root = root->Left ; Else if(Root->val < min (p->val, q->val)) root = root->Right ; Else  Break; }              returnRoot; }};

[Leetcode] Lowest Common Ancestor of a binary search tree two forks the smallest common parent node

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.