一、剑指 Offer 64. 求1+2+…+n
地址
class Solution {
public int sumNums(int n) {
int i=1;
try{
i=i/n;
}catch(Exception e){
return 0;
}
return n+sumNums(n-1);
}
}
二、剑指 Offer 68 - I. 二叉搜索树的最近公共祖先
地址
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root==null){
return null;
}
if(p==root||q==root){
return root;
}
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);
}
return root;
}
}