Yes, when looking at this problem, I was careless and didn't notice the tree is binary search tree. And I also solve this problem, so I want to share with you.

**The following is my ideas and code :**

traversal the tree

`1) if find two p and q, return 2`

`2) if find one of p and q, return 1 and then rollback to the parent node, the common node also rollback to the parent node`

`3) if find nothing of p or q, return 0 and do nothing to common node.`

```
private TreeNode common = null;
/**
*
* 27 / 27 test cases passed.
* Status: Accepted
* Runtime: 9 - 10ms, bit 26.45% - 14.05%
*
* @param root
* @param p
* @param q
* @return
*/
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
helper(root, p, q);
return common;
}
/**
* @param root
* @param p
* @param q
* @return
*/
private int helper(TreeNode root, TreeNode p, TreeNode q) {
int count = 0;
if (root == null) return count;
if (root.val == p.val || root.val == q.val) {
if (common == null) {
common = root;
count = 1;
} else return 2;
}
int res = helper(root.left, p, q);
if (res == 1) common = root;
else if (res == 2) return res;
return count + res + helper(root.right, p, q);
}
```