My java solution , easy to understand

  • 0
    public class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(p.val>q.val) return lowestCommonAncestor(root,q,p);
        if(root==null) return null;
        if(p.val>root.val) return lowestCommonAncestor(root.right,p,q);
        if(q.val<root.val) return lowestCommonAncestor(root.left,p,q);
        return root;


Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.