Lowest Common Ancestor of a Binary Search Tree Solutions in Java
Number 235
Difficulty Easy
Acceptance 50.0%
Link LeetCode
Solutions
Java solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/description//// Author : liuyubobobo/// Time : 2017-11-18/// Recursive/// Time Complexity: O(lgn), where n is the node's number of the tree/// Space Complexity: O(h), where h is the height of the treeclass Solution {public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {if(p == null || q == null)throw new IllegalArgumentException("p or q can not be null.");if(root == null)return null;if(p.val < root.val && q.val < root.val)return lowestCommonAncestor(root.left, p, q);if(p.val > root.val && q.val > root.val)return lowestCommonAncestor(root.right, p, q);assert p.val == root.val || q.val == root.val|| (root.val - p.val) * (root.val - q.val) < 0;return root;}}
Java solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/description//// Author : liuyubobobo/// Time : 2018-12-16/// Non-Recursive/// Time Complexity: O(lgn), where n is the node's number of the tree/// Space Complexity: O(1)class Solution2 {public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {if(root == null)return root;TreeNode cur = root;while(cur != null){if(p.val < cur.val && q.val < cur.val)cur = cur.left;else if(p.val > cur.val && q.val > cur.val)cur = cur.right;elsereturn cur;}return null;}}