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 nodes2
And8
Is6
. Another example is LCA of nodes2
And4
Is2
, 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.