草庐IT

Java二叉树进阶面试题讲解

晓星航 2023-07-04 原文

Java二叉树进阶面试题讲解

大家好,我是晓星航。今天为大家带来的是 Java二叉树进阶面试题讲解 的讲解!😀

🍏1.二叉树的构建及遍历🍏

二叉树的构建及遍历。OJ链接

示例图解:

import java.util.*;
class TreeNode {
    public char val;
    public TreeNode left;
    public TreeNode right;
    public TreeNode(char val) {
        this.val = val;
    }
}
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static int i = 0;
    public static TreeNode createTree(String str) {
        TreeNode root = null;
        if (str.charAt(i) != '#') {
            root = new TreeNode(str.charAt(i));
            i++;
            root.left = createTree(str);
            root.right = createTree(str);
        } else {
            //遇到# 就是空树
            i++;
        }
        return root;
    }
    public static void inorder(TreeNode root) {
        if (root == null) {
            return;
        }
        inorder(root.left);
        System.out.print(root.val + " ");
        inorder(root.right);
    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String str = in.nextLine();
            TreeNode root = createTree(str);
            inorder(root);
        }
    }
}

思路:根据题目意思:我们的#是null即为空结点的意思,因此我们再使用str.CharAt(i)来遍历我们输入的每一个字符时遇到#就直接i++,即使这一个结点的左结点置为空,如果继续遇到#就继续i++使其右节点也为空,然后返回我们的上一个结点。直到遍历完整个字符串我们的树便算是创建完毕了。

🍎2.二叉树的分层遍历🍎

二叉树的分层遍历 OJ链接

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
                List<List<Integer>> ret = new ArrayList<>();
        if (root == null) {
            return ret;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            int size = queue.size();//这个值代表当前行有多少个结点
            List<Integer> list = new ArrayList<>();
            while (size != 0) {
                TreeNode cur = queue.poll();
                list.add(cur.val);
                if (cur.left != null) {
                    queue.offer(cur.left);
                }
                if (cur.right != null) {
                    queue.offer(cur.right);
                }
                size--;
            }
            ret.add(list);
        }
        return ret;
    }
}

思路:首先判断树是否为空,为空直接返回链表对象ret,不为空继续往下走,将root根结点添加进入queue队列,并每次拿出一个元素给cur,将cur的值添加到list中,并访问cur的左右结点继续循环直至root树为空,最后返回我们的ret即可。

🍊3.给定一个二叉树, 找到该树中两个指定节点的最近公共祖先🍊

给定一个二叉树, 找到该树中两个指定节点的最近公共祖先 。OJ链接

二叉搜索树:根的左边比根小,根的右边比根大 中序遍历的大小是有序的。

思路一:以二叉搜索树为例来讲解此题

1、root == p || root == q 此时的最近公共祖先是root

2、p.val < root.val || q.val < root.val p和q都在root的左子树 最近公共祖先在root的左树当中

3、p.val > root.val || q.val > root.val p和q都在root的右子树 最近公共祖先在root的右树当中

4、p.val > root.val && q.val < root.val q和p分别在root的左子树和右子树当中 最近公共祖先就是root

三种情况的图解:

/**
 * 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 leftT = lowestCommonAncestor(root.left,p,q);
        TreeNode rightT = lowestCommonAncestor(root.right,p,q);
        if (leftT != null && rightT != null) {
            return root;
        } else if(leftT != null) {
            return leftT;
        } else if(rightT != null) {
            return rightT;
        } else {
            return null;
        }
    }
}

当root为3,p为6,q为4时,我们代码运行的逻辑图解:

注:每一次return是返回上一个函数的值,直到第一个递归函数return才是返回我们程序的值。

思路二:假设 这棵二叉树 是使用孩子双亲表示法 表示的

1、用两个栈 存储 路径 — 如何找到从根结点到指定结点的路径

2、求栈的大小

3、计算出两个栈中 多的元素 出差值个元素

4、开始出栈 直到栈顶元素相同 此时就是公共祖先

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

    //root:根节点    node:指定的节点  stack:存放指定节点的路径
    public boolean getPath (TreeNode root,TreeNode node,Stack<TreeNode> stack) {
        if(root == null || node == null) {
            return false;
        }
        stack.push(root);
        if(root == node) {
            return true;
        }
        boolean flg = getPath(root.left,node,stack);
        if(flg == true) {
            return true;
        }
        flg = getPath(root.right,node,stack);
        if(flg == true) {
            return true;
        }
        stack.pop();
        return false;
    }
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root == null) {
            return null;
        }
        Stack<TreeNode> stack1 = new Stack<>();
        getPath(root,p,stack1);
        Stack<TreeNode> stack2 = new Stack<>();
        getPath(root,q,stack2);
        int size1 = stack1.size();
        int size2 = stack2.size();
        if (size1 > size2) {
            int size = size1 - size2;
            while (size != 0) {
                //出第一个栈里面的元素
                stack1.pop();
                size--;
            }
            while(!stack1.isEmpty() && !stack2.isEmpty()) {
                //判断地址
                if (stack1.peek() == stack2.peek()) {
                    return stack1.pop();
                } else {
                    stack1.pop();
                    stack2.pop();
                }
            }
        } else {
        int size = size2 - size1;
            while (size != 0) {
                stack2.pop();
                size--;
            }
            while(!stack1.isEmpty() && !stack2.isEmpty()) {
                //判断地址
                if (stack1.peek() == stack2.peek()) {
                    return stack1.pop();
                } else {
                    stack1.pop();
                    stack2.pop();
                }
            }
        }

        return null;
    }
}

🍌4.二叉树搜索树转换成排序双向链表🍌

二叉树搜索树转换成排序双向链表OJ链接

思考问题:

1.排序:可以中序遍历这棵二叉搜索树

2.双向链表:如何构建前驱和后续结点

思路:left变为双向链表的前驱

right变为双向链表的后继

/**
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}
*/
public class Solution {
    TreeNode prev = null;
    public void inorder (TreeNode pCur) {
        if (pCur == null) {
            return;
        }
        inorder(pCur.left);//左

        //先判断左 给左节点赋关系
        pCur.left = prev;
        //在左走完以后 判断prev是否为空 并给右节点赋关系
        if (prev != null) {
            prev.right = pCur;
        }
        //在这个节点的左右关系都确定好后将prev变成pCur
        prev = pCur;
//      System.out.print(pCur.val + " ");
        //然后开始进入右节点递归
        inorder(pCur.right);//右
    }
    public TreeNode Convert(TreeNode pRootOfTree) {
        if (pRootOfTree == null) {
            return null;
        }
        inorder(pRootOfTree);
        TreeNode head = pRootOfTree;
        //这个while的作用是找到这棵树的最下的左节点 这个左节点就是我们需要找到的节点
        while (head.left != null) {
            head = head.left;
        }
        return head;
    }
}

🍉5.根据一棵树的前序遍历与中序遍历构造二叉树🍉

根据一棵树的前序遍历与中序遍历构造二叉树 OJ链接

思路:

1、先将pi下标的 元素 创建为root

2、在中序遍历的数组当中,找到当前pi下标的元素,存在的位置。ri

3、root.left = ri - 1;

root.right = ri + 1;

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int preIndex = 0;
    public TreeNode creatTreeByPandI(int[] preorder,int[] inorder, int inbegin, int inend) {
        if(inbegin > inend) {
            //如果满足这个条件 说明 没有左树 或者 右树了
            return null;
        }
        TreeNode root = new TreeNode(preorder[preIndex]);
        //找到根在中序遍历的位置
        int rootIndex = findIndexOfI(inorder,inbegin,inend,preorder[preIndex]);
        if (rootIndex == -1) {
            return null;
        }
        preIndex++;
        //分别找到左子树和右子树
        root.left = creatTreeByPandI(preorder,inorder,inbegin,rootIndex - 1);
        root.right = creatTreeByPandI(preorder,inorder,rootIndex + 1,inend);
        return  root;
    }
    private int findIndexOfI(int[] inorder, int inbegin, int inend,int key) {
        for (int i = inbegin; i <= inend; i++) {
            if (inorder[i] == key) {
                return i;
            }
        }
        return -1;
    }
    public TreeNode buildTree(int[] preorder, int[] inorder) {
                if (preorder == null || inorder == null) {
            return null;
        }
        return creatTreeByPandI(preorder,inorder,0,inorder.length-1);
    }
}

🍇6.根据一棵树的中序遍历与后序遍历构造二叉树🍇

根据一棵树的中序遍历与后序遍历构造二叉树OJ链接

思路:

1、先将pi下标的 元素 创建为root

2、在中序遍历的数组当中,找到当前pi下标的元素,存在的位置。ri

3、先找根,然后在找右子树,最后找左子树。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int postIndex = 0;
    public TreeNode creatTreeByPandI(int[] inorder,int[] postorder, int inbegin, int inend) {
        if(inbegin > inend) {
            //如果满足这个条件 说明 没有左树 或者 右树了
            return null;
        }
        TreeNode root = new TreeNode(postorder[postIndex]);
        //找到根在中序遍历的位置
        int rootIndex = findIndexOfI(inorder,inbegin,inend,postorder[postIndex]);
        if (rootIndex == -1) {
            return null;
        }
        postIndex--;
        //分别找到右子树和左子树
        root.right = creatTreeByPandI(inorder,postorder,rootIndex + 1,inend);
        root.left = creatTreeByPandI(inorder,postorder,inbegin,rootIndex - 1);
        return  root;
    }
    private int findIndexOfI(int[] inorder, int inbegin, int inend,int key) {
        for (int i = inbegin; i <= inend; i++) {
            if (inorder[i] == key) {
                return i;
            }
        }
        return -1;
    }
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        if (postorder == null || inorder == null) {
            return null;
        }
        postIndex = postorder.length-1;

        return creatTreeByPandI(inorder,postorder,0,inorder.length-1);
    }
}

🍓7.二叉树创建字符串🍓

二叉树创建字符串OJ链接

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public void treeToString(TreeNode t,StringBuilder sb) {
        if (t == null) {
            return;
        }
        sb.append(t.val);
        if (t.left != null) {
            sb.append("(");
            treeToString(t.left,sb);
            sb.append(")");
        } else {
            //t.left == null;
            if (t.right == null) {
                return;
            } else {
                sb.append("()");
            }
        }
        if (t.right == null) {
            return;
        } else {
            sb.append("(");
            treeToString(t.right,sb);
            sb.append(")");
        }
        
    }
    public String tree2str(TreeNode root) {
        if (root == null) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        treeToString(root,sb);
        return sb.toString();
    }
}

思路:例如上图,我们一直往左,每添加一个元素就加一个"(“,再往右判断如果右也为空我们就添加一个”)“,然后返回到上一个元素,如果右边有元素也是重复之前的操作,添加一个”(“然后继续往后判断,左右为空就添加一个”)“,如果左树为空右树不为空,我们就添加一个”()"。

感谢各位读者的阅读,本文章有任何错误都可以在评论区发表你们的意见,我会对文章进行改正的。如果本文章对你有帮助请动一动你们敏捷的小手点一点赞,你的每一次鼓励都是作者创作的动力哦!😘

有关Java二叉树进阶面试题讲解的更多相关文章

  1. java - 等价于 Java 中的 Ruby Hash - 2

    我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/

  2. java - 从 JRuby 调用 Java 类的问题 - 2

    我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www

  3. java - 我的模型类或其他类中应该有逻辑吗 - 2

    我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我

  4. java - 什么相当于 ruby​​ 的 rack 或 python 的 Java wsgi? - 2

    什么是ruby​​的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht

  5. 世界前沿3D开发引擎HOOPS全面讲解——集3D数据读取、3D图形渲染、3D数据发布于一体的全新3D应用开发工具 - 2

    无论您是想搭建桌面端、WEB端或者移动端APP应用,HOOPSPlatform组件都可以为您提供弹性的3D集成架构,同时,由工业领域3D技术专家组成的HOOPS技术团队也能为您提供技术支持服务。如果您的客户期望有一种在多个平台(桌面/WEB/APP,而且某些客户端是“瘦”客户端)快速、方便地将数据接入到3D应用系统的解决方案,并且当访问数据时,在各个平台上的性能和用户体验保持一致,HOOPSPlatform将帮助您完成。利用HOOPSPlatform,您可以开发在任何环境下的3D基础应用架构。HOOPSPlatform可以帮您打造3D创新型产品,HOOPSSDK包含的技术有:快速且准确的CAD

  6. Observability:从零开始创建 Java 微服务并监控它 (二) - 2

    这篇文章是继上一篇文章“Observability:从零开始创建Java微服务并监控它(一)”的续篇。在上一篇文章中,我们讲述了如何创建一个Javaweb应用,并使用Filebeat来收集应用所生成的日志。在今天的文章中,我来详述如何收集应用的指标,使用APM来监控应用并监督web服务的在线情况。源码可以在地址 https://github.com/liu-xiao-guo/java_observability 进行下载。摄入指标指标被视为可以随时更改的时间点值。当前请求的数量可以改变任何毫秒。你可能有1000个请求的峰值,然后一切都回到一个请求。这也意味着这些指标可能不准确,你还想提取最小/

  7. 【Java 面试合集】HashMap中为什么引入红黑树,而不是AVL树呢 - 2

    HashMap中为什么引入红黑树,而不是AVL树呢1.概述开始学习这个知识点之前我们需要知道,在JDK1.8以及之前,针对HashMap有什么不同。JDK1.7的时候,HashMap的底层实现是数组+链表JDK1.8的时候,HashMap的底层实现是数组+链表+红黑树我们要思考一个问题,为什么要从链表转为红黑树呢。首先先让我们了解下链表有什么不好???2.链表上述的截图其实就是链表的结构,我们来看下链表的增删改查的时间复杂度增:因为链表不是线性结构,所以每次添加的时候,只需要移动一个节点,所以可以理解为复杂度是N(1)删:算法时间复杂度跟增保持一致查:既然是非线性结构,所以查询某一个节点的时候

  8. 【Java入门】使用Java实现文件夹的遍历 - 2

    遍历文件夹我们通常是使用递归进行操作,这种方式比较简单,也比较容易理解。本文为大家介绍另一种不使用递归的方式,由于没有使用递归,只用到了循环和集合,所以效率更高一些!一、使用递归遍历文件夹整体思路1、使用File封装初始目录,2、打印这个目录3、获取这个目录下所有的子文件和子目录的数组。4、遍历这个数组,取出每个File对象4-1、如果File是否是一个文件,打印4-2、否则就是一个目录,递归调用代码实现publicclassSearchFile{publicstaticvoidmain(String[]args){//初始目录Filedir=newFile("d:/Dev");Datebeg

  9. Hive SQL 五大经典面试题 - 2

    目录第1题连续问题分析:解法:第2题分组问题分析:解法:第3题间隔连续问题分析:解法:第4题打折日期交叉问题分析:解法:第5题同时在线问题分析:解法:第1题连续问题如下数据为蚂蚁森林中用户领取的减少碳排放量iddtlowcarbon10012021-12-1212310022021-12-124510012021-12-134310012021-12-134510012021-12-132310022021-12-144510012021-12-1423010022021-12-154510012021-12-1523.......找出连续3天及以上减少碳排放量在100以上的用户分析:遇到这类

  10. java - 为什么 ruby​​ modulo 与 java/other lang 不同? - 2

    我基本上来自Java背景并且努力理解Ruby中的模运算。(5%3)(-5%3)(5%-3)(-5%-3)Java中的上述操作产生,2个-22个-2但在Ruby中,相同的表达式会产生21个-1-2.Ruby在逻辑上有多擅长这个?模块操作在Ruby中是如何实现的?如果将同一个操作定义为一个web服务,两个服务如何匹配逻辑。 最佳答案 在Java中,模运算的结果与被除数的符号相同。在Ruby中,它与除数的符号相同。remainder()在Ruby中与被除数的符号相同。您可能还想引用modulooperation.

随机推荐