剑指offer 64.68Ⅰ.68Ⅱ. 搜索与回溯算法(中等)

64.

题目:

剑指 Offer 64. 求1+2+…+nhttps://leetcode-cn.com/problems/qiu-12n-lcof/

剑指offer 64.68Ⅰ.68Ⅱ. 搜索与回溯算法(中等)_第1张图片

想法:使用递归.但是终止递归需要if,能不能不用if? -> 使用&&短路

代码:

class Solution {
    public int sumNums(int n) {
        //if终止递归(题目不让)
        if(n<=0) return 0;
        return n+sumNums(n-1);
    }
}

class Solution {
    public int sumNums(int n) {
        //短路终止递归
        boolean x = n > 1 && (n += sumNums(n - 1)) > 0;
        return n;
    }
}

结果:

剑指offer 64.68Ⅰ.68Ⅱ. 搜索与回溯算法(中等)_第2张图片

68Ⅰ.

题目:

剑指 Offer 68 - I. 二叉搜索树的最近公共祖先https://leetcode-cn.com/problems/er-cha-sou-suo-shu-de-zui-jin-gong-gong-zu-xian-lcof/

剑指offer 64.68Ⅰ.68Ⅱ. 搜索与回溯算法(中等)_第3张图片

 剑指offer 64.68Ⅰ.68Ⅱ. 搜索与回溯算法(中等)_第4张图片

想法:借助二叉搜索树的性质,判断pq是否分属root两端.

代码:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */

//我写的
class Solution {
    TreeNode node;
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        //使pq.val)
            findNode(root,q,p);
        else
            findNode(root,p,q);
            return node;
    }
    private void findNode(TreeNode root, TreeNode p, TreeNode q){
        if(root==null)
            return;
        if(p.val>root.val)//说明都在右边
            lowestCommonAncestor(root.right,p,q);
        if(q.val p.val && root.val > q.val)
            return lowestCommonAncestor(root.left, p, q);
        //否则pq分处于root的两边
        return root;
    }
}

结果:

剑指offer 64.68Ⅰ.68Ⅱ. 搜索与回溯算法(中等)_第5张图片

68Ⅱ.

题目:

剑指 Offer 68 - II. 二叉树的最近公共祖先https://leetcode-cn.com/problems/er-cha-shu-de-zui-jin-gong-gong-zu-xian-lcof/

剑指offer 64.68Ⅰ.68Ⅱ. 搜索与回溯算法(中等)_第6张图片

剑指offer 64.68Ⅰ.68Ⅱ. 搜索与回溯算法(中等)_第7张图片

想法:与上题类似,但不是二叉搜索树了,建立map映射中序遍历下标,下标仍有二叉搜索树的性质. 结果应该对,但是超时.

代码: 

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    TreeNode node;
    int index=0;
    Map map = new HashMap<>();
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        recur(root);
        //使pmap.get(q.val))
            findNode(root,q,p);
        else
            findNode(root,p,q);
        return node;
    }    
    //中序遍历
    private void recur(TreeNode root){
        if(root==null) 
            return;
        recur(root.left);
        index++;
        map.put(root.val,index);
        recur(root.right);   
    }
    private void findNode(TreeNode root, TreeNode p, TreeNode q){
        if(root==null)
            return;
        if(map.get(p.val)>map.get(root.val))//说明都在右边
            lowestCommonAncestor(root.right,p,q);
        if(map.get(q.val)

参考k神:

/**
 * 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) {
        // 如果树为空,直接返回null
        if(root == null) 
            return null; 
        // 如果 p 和 q 中有等于 root 的,那么它们的最近公共祖先即为root(一个节点也可以是它自己的祖先)
        if(root == p || root == q) 
            return root;   
      
        // 递归遍历左子树,只要在左子树中找到了p或q,则先找到谁就返回谁
        TreeNode left = lowestCommonAncestor(root.left, p, q); 
        // 递归遍历右子树,只要在右子树中找到了p或q,则先找到谁就返回谁
        TreeNode right = lowestCommonAncestor(root.right, p, q); 

        // 情况3: 如果在左子树中 p 和 q 都找不到,则 p 和 q 一定都在右子树中,右子树中先遍历到的那个就是最近公共祖先(一个节点也可以是它自己的祖先)
        if(left == null) 
            return right; 
        // 情况4: 如果 left 不为空,在左子树中有找到节点(p或q),这时候要再判断一下右子树中的情况,如果在右子树中,p和q都找不到,则 p 和 q 一定都在左子树中,左子树中先遍历到的那个就是最近公共祖先(一个节点也可以是它自己的祖先)
        if(right == null) 
            return left; 
        // 情况1: left 和 right 同时为空, 说明 root 的左 / 右子树中都不包含 p q ,返回 null
        if(left == null && right== null) 
            return null;
        // 情况2: left 和 right 均不为空时,说明 p、q节点分别在 root异侧, 最近公共祖先即为 root
        // if(left != null and right != null)
        return root; 
    }
}

结果:

剑指offer 64.68Ⅰ.68Ⅱ. 搜索与回溯算法(中等)_第8张图片

 

你可能感兴趣的:(leetcode-Code,算法,leetcode,深度优先,java)