class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { //root节点的值 大于p、q俩个节点,说明公共祖先在根节点的左侧 if(root.val > p.val && root.val > q.val){ return lowestCommonAncestor(root.left,p,q); } //root节点得值 小于p、q俩个节点,说明公共祖先在根节点的右侧 if(root.val < p.val && root.val < q.val){ return lowestCommonAncestor(root.right,p,q); } //其余情况 只剩 p、q在 root俩侧,说明是公共祖先 return root; } }