面试经典150题(72-77)

leetcode 150道题 计划花两个月时候刷完,今天(第三十五天)完成了6道(72-77)150:

72.(236. 二叉树的最近公共祖先) 题目描述:

给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。
百度百科中最近公共祖先的定义为:“对于有根树 T 的两个节点 p、q,最近公共祖先表示为一个节点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。”

第一版(去查了百度百科,然后就有了我的一坨代码。。。)

class Solution {
    
    
    Map<TreeNode,List<TreeNode>> map=new HashMap();
    TreeNode grandTreeNode=null;
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
    
    
        if(p==q){
    
    
            return p;
        }
        dfs(root,p,q);
        return grandTreeNode;
    }
    public void dfs(TreeNode root, TreeNode p, TreeNode q) {
    
    
        if(root==null){
    
    
            return ;
        }
        dfs(root.left,p,q);
        dfs(root.right,p,q);
        if(grandTreeNode!=null){
    
    
            return;
        }
        List<TreeNode> temp=new ArrayList();
        if(root.left!=null&&map.get(root.left)!=null){
    
    
            temp.addAll(map.get(root.left));
            temp.add(root.left);
        }
        if(root.right!=null&&map.get(root.right)!=null){
    
    
            temp.addAll(map.get(root.right));
            temp.add(root.right);
        }
        
        if(p==root&&temp.contains(q)){
    
    
            grandTreeNode=p;
            return;
        }
        if(q==root&&temp.contains(p)){
    
    
            grandTreeNode=q;
            return;
        }
        if(temp.contains(p)&&temp.contains(q)){
    
    
            grandTreeNode=root;
            return;
        }
        map.put(root,temp);
    }
}

第二版(看了解题也没看懂,然后看评论区有个好理解的版本就试了一下,真不错,谁先找到谁就是头,都没找到那就是上一个递归)

class Solution {
    
    
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
    
    
        if(root==null||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){
    
    
            return root;
        }else if(left!=null){
    
    
            return left;
        }else if(right!=null){
    
    
            return right;
        }
        return null;
    }
}

73-76 全是 二叉树的层次遍历,我放下面,先放 77

77.(530. 二叉搜索树的最小绝对差)题目描述:

给你一个二叉搜索树的根节点 root ,返回 树中任意两不同节点值之间的最小差值 。
差值是一个正数,其数值等于两值之差的绝对值。

第一版(看重点,是二叉搜索树,他的中序遍历是有序的,我不信这题是简单题。。。)

class Solution {
    
    
    public int getMinimumDifference(TreeNode root) {
    
    
        //中序遍历
        if(root==null){
    
    
            return 0;
        }
        TreeNode preNode=null;
        int min=Integer.MAX_VALUE;
        Stack<TreeNode> stack=new Stack();
        while(!stack.isEmpty()||root!=null){
    
    
            while(root!=null){
    
    
                stack.push(root);
                root=root.left;
            }
            root=stack.pop();
            if(preNode!=null)
            {
    
    
                min=Math.min(min,Math.abs(preNode.val-root.val));
            }
            preNode=root;
            root=root.right;
        }
        return min;
    }
}

73.(199. 二叉树的右视图)题目描述:

给定一个二叉树的 根节点 root,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。

第一版(就是层次遍在这里插入代码片历,每次输出这一层的最后一个)

class Solution {
    
    
    public List<Integer> rightSideView(TreeNode root) {
    
    
        List<Integer> res=new ArrayList();
        if(root==null){
    
    
            return res;
        }
        //层次遍历 输出最后一个
        LinkedList<TreeNode> queue=new LinkedList();
        queue.add(root);
        while(!queue.isEmpty()){
    
    
            res.add(queue.peekLast().val);
            int currNum=queue.size();
            while(currNum!=0){
    
    
                TreeNode temp= queue.poll();
                if(temp.left!=null){
    
    
                    queue.add(temp.left);
                }
                if(temp.right!=null){
    
    
                    queue.add(temp.right);
                }
                currNum--;
            }
        }
        return res;
    }
}

74.(637. 二叉树的层平均值)题目描述:

给定一个非空二叉树的根节点 root , 以数组的形式返回每一层节点的平均值。与实际答案相差 10-5 以内的答案可以被接受。

第一版(还是层次遍历)

class Solution {
    
    
    public List<Double> averageOfLevels(TreeNode root) {
    
    
        List<Double> res=new ArrayList();
        if(root==null){
    
    
            return res;
        }
        Queue<TreeNode> queue=new LinkedList();
        queue.offer(root);
        while(!queue.isEmpty()){
    
    
            int currNum=queue.size();
            // 不为double 加着加着就超范围了
            double sum=0;
            for(int i=0;i<currNum;i++){
    
    
                TreeNode temp=queue.poll();
                sum+=temp.val;
                if(temp.left!=null){
    
    
                    queue.offer(temp.left);
                }
                if(temp.right!=null){
    
    
                    queue.offer(temp.right);
                }
            }
            res.add(sum/currNum);
        }
        return res;

    }
}

75.(102. 二叉树的层序遍历)题目描述:

给你二叉树的根节点 root ,返回其节点值的 层序遍历 。 (即逐层地,从左到右访问所有节点)。

第一版(这个最直接。。)

class Solution {
    
    
    public List<List<Integer>> levelOrder(TreeNode root) {
    
    
        List<List<Integer>> res=new ArrayList();
        if(root==null){
    
    
            return res;
        }
        Queue<TreeNode> queue=new LinkedList();
        queue.offer(root);
        while(!queue.isEmpty()){
    
    
            int currNum=queue.size();
            List<Integer> tempList=new ArrayList();
            for(int i=0;i<currNum;i++){
    
    
                TreeNode temp=queue.poll();
                tempList.add(temp.val);
                if(temp.left!=null){
    
    
                    queue.offer(temp.left);
                }
                if(temp.right!=null){
    
    
                    queue.offer(temp.right);
                }
            }
            res.add(tempList);
        }
        return res;
    }
}

76.(103. 二叉树的锯齿形层序遍历)题目描述:

给你二叉树的根节点 root ,返回其节点值的 锯齿形层序遍历 。(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行)。

第一版(还是层次遍历,第一层从左向右,第二层从右向左。。。)

class Solution {
    
    
    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
    
    
        List<List<Integer>> res=new ArrayList();
        if(root==null){
    
    
            return res;
        }
        boolean flag=true;
        Queue<TreeNode> queue=new LinkedList();
        queue.offer(root);
        while(!queue.isEmpty()){
    
    
            int currNum=queue.size();
            List<Integer> tempList=new ArrayList();
            for(int i=0;i<currNum;i++){
    
    
                TreeNode temp=queue.poll();
                if(flag)
                    tempList.add(temp.val);
                else
                    tempList.add(0,temp.val);
                if(temp.left!=null){
    
    
                    queue.offer(temp.left);
                }
                if(temp.right!=null){
    
    
                    queue.offer(temp.right);
                }
            }
            flag=!flag;
            res.add(tempList);
        }
        return res;
    }
}

算是复习了一下,二叉树的遍历写法,递归和非递归的都有。。

第三十五天了,加油早日跳槽!!!

猜你喜欢

转载自blog.csdn.net/weixin_44061648/article/details/135467909
今日推荐