Likou 235 The nearest common ancestor in the binary search tree

Title link: https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-search-tree

Given a binary search tree, find the nearest common ancestor of two specified nodes in the tree.

The definition of the nearest common ancestor in Baidu Encyclopedia is: "For the two nodes p and q of the rooted tree T, the nearest common ancestor is expressed as a node x, so that x is the ancestor of p and q and the depth of x is as large as possible (A node can also be its own ancestor)."

For example, given the following binary search tree: root = [6,2,8,0,4,7,9,null,null,3,5]

 

Example 1:

Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8
Output: 6 
Explanation: The nearest common ancestor of node 2 and node 8 is 6.
Example 2:

Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4
Output: 2
Explanation: The nearest common ancestor of node 2 and node 4 is 2, Because according to the definition, the nearest common ancestor node can be the node itself.
 

Description:

  • The values ​​of all nodes are unique.
  • p and q are different nodes and both exist in the given binary search tree.

 

        It's actually very simple. Note that the request is to return the node, not the value. Because it is a binary search tree, the values ​​of all nodes in the left subtree of a node are less than or equal to the value of this point, and the values ​​of all nodes in the left subtree are greater than or equal to the value of this point. The first in the original title description Article "The value of all nodes is unique" then there is no equal to the above situation. That is to say, for the root node, if the value of any node is less than its value, then the node is located in its left subtree, and if it is greater than it is located in the right subtree. If two nodes are located in the left and right subtrees of the root node (that is, the value of the root node is between the values ​​of the two nodes), it is obvious that their nearest common ancestor is the root node; otherwise, the two nodes are located at the same time In the left and right subtrees of the root node (that is, both nodes are larger or smaller than the value of the root node), then the nearest common ancestor of the two nodes is also located in the left and right subtrees, and the value of the nearest common ancestor is introduced Between the values ​​of the two nodes.

        The algorithm can be completed by recursion/iteration. The input is the root node of the subtree, the two nodes p and q, and let vallow be the minimum value of the two node values ​​of p and q, and valhigh is the maximum value, then:

  1. If the value of root is greater than or equal to the value of vallow but less than or equal to the value of valhigh, it means that it is between the two values, and the two nodes are located in its left and right subtrees respectively, then root is their nearest common ancestor;
  2. Otherwise, if the value of root is greater than the value of valhigh, it means that the values ​​of the two nodes are both smaller than the value of root, that is, both are located in the left subtree, then enter the left subtree of root to search;
  3. Otherwise, the value of root is less than the value of vallow, indicating that the values ​​of the two nodes are greater than the value of root, that is, both are located in the right subtree, and then enter the right subtree of root to search.

       The reason for the equal sign on both sides in case 1 is that the nearest common ancestor node can be the node itself, so if there is an equality, it means that one of the two nodes is the root node.

Recursive method:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */

class Solution {
public:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        int vallow=min(p->val,q->val);
        int valhigh=max(p->val,q->val);
        if (vallow<=root->val && valhigh>=root->val)
            return root;
        else if(valhigh<root->val)
            return lowestCommonAncestor(root->left,p,q);
        else
            return lowestCommonAncestor(root->right,p,q);
        }
};

Iterative method:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */

class Solution {
public:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        TreeNode* r=root;
        int vallow=min(p->val,q->val);
        int valhigh=max(p->val,q->val);
        while(r){
            if (vallow<=r->val && valhigh>=r->val)
                break;//其实这行可以直接return r的,但是不通过
            else if(valhigh<r->val)
                r=r->left;
            else
                r=r->right;
        }
        return r;
    }
};

 

Guess you like

Origin blog.csdn.net/qq_36614557/article/details/108835718