150 classic interview questions (72-77)

It is planned to take two months to complete the 150 questions of leetcode. Today (the 35th day), I completed 6 questions (72-77) 150:

72. (236. The most recent common ancestor of a binary tree) Question description:

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

The first version (I checked Baidu Encyclopedia, and then I got a bunch of code...)

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);
    }
}

The second version (I didn’t understand it even after looking at the solution, and then I tried it after seeing a version in the comment area that was easy to understand. It was really good. Whoever finds it first is the leader. If you can’t find it, it’s the previous recursion)

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 are all level traversals of binary trees. I will put them below, starting with 77.

77. (530. Minimum absolute difference of binary search tree) Question description:

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

First edition (the key point is that it is a binary search tree, and its inorder traversal is ordered. I don’t believe this question is a simple question...)

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. Right view of a binary tree) Question description:

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

The first version (that is, layer 在这里插入代码片traversal , outputting the last one of this layer each time)

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. Level average of binary tree) Question description:

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

The first version (still level traversal)

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. Level-order traversal of binary trees) Question description:

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

The first version (this is the most direct...)

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. Zigzag layer-order traversal of binary tree) Problem description:

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

The first version (still layer traversal, the first layer is from left to right, the second layer is from right to left...)

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;
    }
}

It's a refresher. There are both recursive and non-recursive ways to write binary tree traversal. .

It’s the thirty-fifth day, come on, quit as soon as possible! ! !

Supongo que te gusta

Origin blog.csdn.net/weixin_44061648/article/details/135467909
Recomendado
Clasificación