剑指 Offer 64. 求1+2+…+n

题目描述

求1+2+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。

方法一:短路与终止递归

根据题意可以使用递归进行实现,但不能使用条件判断相关的关键字和(A?B:C)判断递归终止条件,解决方法是采用短语与的特性,前一个判断若不成立,那么后一个判断将不会执行
class Solution {
    public int sumNums(int n) {
        //通过短路与终止递归,即当n <= 1时后面的判断将不会得到执行
        boolean flag = n > 1 && (n += sumNums(n - 1)) > 0;
        return n;
    }
}

方法二:Marh.pow()+位运算

其实也是使用了乘除法的公式(n^2 + n) / 2,但通过函数调用和位运算替代了常规的乘除过程
class Solution {
    public int sumNums(int n) {
        return (int)(Math.pow(n, 2) + n) >> 1;
    }
}

剑指 Offer 68 - I. 二叉搜索树的最近公共祖先

题目描述

给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先。
百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。”
🔗题目链接:https://leetcode-cn.com/problems/er-cha-sou-suo-shu-de-zui-jin-gong-gong-zu-xian-lcof

代码实现

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        //若最近公共祖先为给定的某个节点,则该节点即为两者的最近公共祖先
        if(root == p || root == q){
            return root;
        }
        if(p.val < root.val && q.val < root.val){ //两个指定节点都在左子树
            return lowestCommonAncestor(root.left, p, q);
        }else if(p.val > root.val && q.val > root.val){ //都在右子树
            return lowestCommonAncestor(root.right, p, q);
        }else{
            //在当前节点的不同子树,则根据二叉搜索树的性质,当前节点即为它们的最近公共祖先
            return root;
        }
    }
}

剑指 Offer 68 - II. 二叉树的最近公共祖先

题目描述

给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。
百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。”
🔗链接:https://leetcode-cn.com/problems/er-cha-shu-de-zui-jin-gong-gong-zu-xian-lcof

思路

代码实现

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root == null){
            return null;
        }
        //当前节点为指定节点就返回当前节点
        if(root == p || root == q){ 
            return root;
        }
        TreeNode left = lowestCommonAncestor(root.left, p, q);
        TreeNode right = lowestCommonAncestor(root.right, p, q);
        if(left != null && right != null){ //p q一个在左,一个在右
            return root;
        }
        //p q有一个在左子树或右子树
        return left != null ? left : right;
    }
}