LeetCode_235. Lowest Common Ancestor of a Binary Search Tree

235. Lowest Common Ancestor of a Binary Search Tree

Easy

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 p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”

Given 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 LCA of nodes 2 and 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 LCA of nodes 2 and 4 is 2, since a node can be a descendant of itself according to the LCA definition.

Note:

  • All of the nodes' values will be unique.
  • p and q are different and both values will exist in the BST.
package leetcode.easy;

public class LowestCommonAncestorOfABinarySearchTree {
	public TreeNode lowestCommonAncestor1(TreeNode root, TreeNode p, TreeNode q) {

		// Value of current node or parent node.
		int parentVal = root.val;

		// Value of p
		int pVal = p.val;

		// Value of q;
		int qVal = q.val;

		if (pVal > parentVal && qVal > parentVal) {
			// If both p and q are greater than parent
			return lowestCommonAncestor1(root.right, p, q);
		} else if (pVal < parentVal && qVal < parentVal) {
			// If both p and q are lesser than parent
			return lowestCommonAncestor1(root.left, p, q);
		} else {
			// We have found the split point, i.e. the LCA node.
			return root;
		}
	}

	public TreeNode lowestCommonAncestor2(TreeNode root, TreeNode p, TreeNode q) {

		// Value of p
		int pVal = p.val;

		// Value of q;
		int qVal = q.val;

		// Start from the root node of the tree
		TreeNode node = root;

		// Traverse the tree
		while (node != null) {

			// Value of ancestor/parent node.
			int parentVal = node.val;

			if (pVal > parentVal && qVal > parentVal) {
				// If both p and q are greater than parent
				node = node.right;
			} else if (pVal < parentVal && qVal < parentVal) {
				// If both p and q are lesser than parent
				node = node.left;
			} else {
				// We have found the split point, i.e. the LCA node.
				return node;
			}
		}
		return null;
	}

	@org.junit.Test
	public void test() {
		TreeNode tn11 = new TreeNode(6);
		TreeNode tn21 = new TreeNode(2);
		TreeNode tn22 = new TreeNode(8);
		TreeNode tn31 = new TreeNode(0);
		TreeNode tn32 = new TreeNode(4);
		TreeNode tn33 = new TreeNode(7);
		TreeNode tn34 = new TreeNode(9);
		TreeNode tn43 = new TreeNode(3);
		TreeNode tn44 = new TreeNode(5);
		tn11.left = tn21;
		tn11.right = tn22;
		tn21.left = tn31;
		tn21.right = tn32;
		tn22.left = tn33;
		tn22.right = tn34;
		tn31.left = null;
		tn31.right = null;
		tn32.left = tn43;
		tn32.right = tn44;
		tn33.left = null;
		tn33.right = null;
		tn34.left = null;
		tn34.right = null;
		tn43.left = null;
		tn44.right = null;
		System.out.println(lowestCommonAncestor1(tn11, tn21, tn22).val);
		System.out.println(lowestCommonAncestor1(tn11, tn21, tn32).val);
		System.out.println(lowestCommonAncestor2(tn11, tn21, tn22).val);
		System.out.println(lowestCommonAncestor2(tn11, tn21, tn32).val);
	}
}

猜你喜欢

转载自www.cnblogs.com/denggelin/p/11748247.html