Leetcode -- Lowest Common Ancestor of a Binary Search Tree, addbinaryleetcode

Source: Internet
Author: User

Leetcode -- Lowest Common Ancestor of a Binary Search Tree, addbinaryleetcode

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

According to the definition of LCA on Wikipedia: "The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants (where we allowA node to be a descendant of itself)."

        _______6______       /              \    ___2__          ___8__   /      \        /      \   0      _4       7       9         /  \         3   5

For example, the lowest common ancestor (LCA) of nodes2And8Is6. Another example is LCA of nodes2And4Is2, Since a node can be a descendant of itself according to the LCA definition.


For a BST tree, give two nodes on the tree to find their closest common ancestor node.

Classification: Binary Tree


Solution 1: because the question is given a BST tree, according to the nature of the BST tree, for a node, the value of all the nodes on the left subtree is smaller than that on it, the value of all nodes on the right tree is greater than that on the tree.

For root nodes, if the p and q nodes are smaller than the root node, the root node is their LCA.

If both are smaller than the root node, it means that their LCA is on the left Tree of the root node. If it is larger than root, it means their LCA is on the right tree of root.

If one of them is equal to the root user, it indicates that this is the LCA.

Based on this idea, we will soon write the code:

/*** Definition for a binary tree node. * public class TreeNode {* int val; * TreeNode left; * TreeNode right; * TreeNode (int x) {val = x ;} *} */public class Solution {public TreeNode lowestCommonAncestor (TreeNode root, TreeNode p, TreeNode q) {if (root = null) return null; if (p. val <root. val & root. val <q. val) {// if one left and one right return root;} else if (p. val> root. val & root. val> q. val) {// if one left and one right return root;} else if (p. val <root. val & root. val> q. val) {// if both are on the left, Recursively search for the left subtree return lowestCommonAncestor (root. left, p, q);} else if (p. val> root. val & q. val> root. val) {// if both are on the right, Recursively search for the right subtree return lowestCommonAncestor (root. right, p, q);} else if (p. val = root. val) {// return p;} else if (q. val = root. val) {// return q;} return null if it is equal to root ;}}

Solution 2: solution 2 is the same as solution 1, but the code is simplified. Except for the left and right subtree, we can return the root

/*** Definition for a binary tree node. * public class TreeNode {* int val; * TreeNode left; * TreeNode right; * TreeNode (int x) {val = x ;} *} */public class Solution {public TreeNode lowestCommonAncestor (TreeNode root, TreeNode p, TreeNode q) {if (root = null) return null; if (p. val <root. val & root. val> q. val) {// if both are on the left, Recursively search for the left subtree return lowestCommonAncestor (root. left, p, q);} else if (p. val> root. val & q. val> root. val) {// if both are on the right, Recursively search for the right subtree return lowestCommonAncestor (root. right, p, q) ;}else {// return root in other cases ;}}}


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.