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 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 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.
Test instructions: For a BST tree, give the two nodes on the tree to find their closest common ancestor node.
Category: Two fork Tree
Solution 1: Because the topic gives the BST tree, according to the properties of the BST tree, for a node, all nodes on the left subtree have a smaller value than it, and the value of all nodes on the right subtree is greater than it.
For root node root, if p,q two is smaller than root, it means that root is their LCA
If they are smaller than root, their LCA is on the left subtree of root. If they are larger than root, their LCA is on the right subtree of root.
If one of them is equal to root, it means that this is LCA.
Based on this idea, we quickly 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 a left-right return root; } else if (p.val>root.val&&root.val>q.val) {//If a left-right return root; } else if (p.val<root.val&&root.val>q.val) {///If all is on the left, recursively finds the left subtree return Lowestcommonancestor ( ROOT.LEFT,P,Q); } else if (p.val>root.val&&q.val>root.val) {///If all is on the right, recursively finds the right subtree return Lowestcommonancestor ( ROOT.RIGHT,P,Q); } else if (p.val==root.val) {//if and root equals return p; } else if (q.val==root.val) {//if and root equals return q; } return null;} }
Solution 2: Solution 2 is the same as the idea of solution 1, but the code is condensed, except in the left and right subtree, the rest of us can return to 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 all is on the left, recursively finds the left subtree return Lowestcommonancestor (root.left,p,q ); } else if (p.val>root.val&&q.val>root.val) {///If all is on the right, recursively finds the right subtree return Lowestcommonancestor ( ROOT.RIGHT,P,Q); } else{//the rest of the situation return root;}}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Leetcode--lowest Common Ancestor of a Binary Search Tree