• [刷题记录]牛客面试笔刷TOP101(二)


    (一)传送门: 

    [刷题记录]牛客面试笔刷TOP101(一)_HY_PIGIE的博客-CSDN博客

    目录

    1.合并二叉树  

    2.二叉树的镜像

    3.判断是否为二叉搜索树

    4.判断是不是完全二叉树


    1.合并二叉树  

    合并二叉树_牛客题霸_牛客网 (nowcoder.com)

    思路:

    在后序遍历的基础上进行,两颗二叉树可能会有位置有空缺的情况.

    在一个子树下,拿到了左节点与右节点并在根节点下进行操作->后序遍历

    1. public TreeNode mergeTrees (TreeNode t1, TreeNode t2) {
    2. // write code here
    3. //如果是两个都是空的也无所谓,相互返回空就好了
    4. if(t1 == null){
    5. return t2;
    6. }
    7. if(t2 == null){
    8. return t1;
    9. }
    10. //创建一个合并的新节点作为根节点
    11. TreeNode root = new TreeNode(t1.val + t2.val);
    12. //进行后序遍历
    13. TreeNode left = mergeTrees(t1.left,t2.left);
    14. TreeNode right = mergeTrees(t1.right,t2.right);
    15. //拼装
    16. root.left = left;
    17. root.right = right;
    18. return root;
    19. }

    2.二叉树的镜像

    二叉树的镜像_牛客题霸_牛客网 (nowcoder.com)

    思路:

    分别交换每一颗子树的左右节点,采用后序遍历的方式

    1. public TreeNode Mirror (TreeNode pRoot) {
    2. // write code here
    3. if(pRoot == null){
    4. return null;
    5. }
    6. //镜像就是交换每一颗子树的左右节点
    7. //那就要先拿到其左右节点
    8. //可以使用后序遍历的方式,后序遍历为:左右根
    9. //当来到根的时候,已经分别拿到了两个节点
    10. //再分别将他们交换位置即可
    11. TreeNode left = Mirror(pRoot.left);
    12. TreeNode right = Mirror(pRoot.right);
    13. pRoot.left = right;
    14. pRoot.right = left;
    15. return pRoot;
    16. }

     3.判断是否为二叉搜索树

    判断是不是二叉搜索树_牛客题霸_牛客网 (nowcoder.com)

    思路:

    二叉搜索树:左节点<根<右节点. 

    可以结合下面这一题一起看

    二叉搜索树与双向链表_牛客题霸_牛客网 (nowcoder.com)

    二叉搜索树的中序遍历则是一个递增的序列,当判断一颗二叉树是否为二叉搜索树时只要判断其中序遍历得到的数值是否为递增就好了.

    既然要比较,就要用到两个指针.

    一个指针指向当前遍历的节点,还有一个指针记录上一个节点的数值.进行比较后移位.

    错解:

    忘记更新pre了....

    1. int pre = Integer.MIN_VALUE;
    2. public boolean isValidBST (TreeNode root) {
    3. // write code here
    4. if(root == null){
    5. return true;
    6. }
    7. //遍历左子树
    8. boolean left = isValidBST(root.left);
    9. //判断前驱的数值与当前节点的数值关系
    10. if(pre > root.val){
    11. return false;
    12. }
    13. boolean right = isValidBST(root.right);
    14. return left && right;
    15. }

     正解:

    1. int pre = Integer.MIN_VALUE;
    2. public boolean isValidBST (TreeNode root) {
    3. // write code here
    4. if(root == null){
    5. return true;
    6. }
    7. //遍历左子树
    8. boolean left = isValidBST(root.left);
    9. //判断前驱的数值与当前节点的数值关系
    10. if(pre > root.val){
    11. return false;
    12. }
    13. //更新一下pre
    14. pre = root.val;
    15. //遍历右子树
    16. boolean right = isValidBST(root.right);
    17. return left && right;
    18. }

     4.判断是不是完全二叉树

    判断是不是完全二叉树_牛客题霸_牛客网 (nowcoder.com)

    思路:

    完全二叉树,不是满二叉树.

    完全二叉树要求可以单独有左节点,但不能单独有右节点.

    满二叉树是除了叶子节点,每一个节点都有左右节点.

    使用层序遍历的方式,遍历二叉树放到队列中.

    当遇到空节点时将其存放,并判断后面的节点是否为空,如果后面的节点不为空则判断其不是完全二叉树

     

    1. public boolean isCompleteTree (TreeNode root) {
    2. // write code here
    3. //层序遍历
    4. //如果有左节点但没有右节点就返回false
    5. Queue queue = new LinkedList<>();
    6. queue.offer(root);
    7. boolean left = true;
    8. while(!queue.isEmpty()){
    9. //当队列不为空时
    10. TreeNode tmp = queue.poll();
    11. //当遇到空节点时
    12. if(tmp == null){
    13. left = false;
    14. }else{//如果当前节点不为空,判断前一个节点是否为空
    15. if(left == false){//如果前一个节点为空,且当前节点不为空
    16. return false;//返回false;
    17. }
    18. //如果前一个节点与后一个节点都不为空
    19. //把其的左右节点加入
    20. queue.offer(tmp.left);
    21. queue.offer(tmp.right);
    22. }
    23. }
    24. return true;
    25. }

    5.判断是不是平衡二叉树

    判断是不是平衡二叉树_牛客题霸_牛客网 (nowcoder.com)

    思路:

    递归的思想,判断每一个根节点的左右子树的高度差.

    1. public boolean IsBalanced_Solution (TreeNode pRoot) {
    2. // write code here
    3. //从上到下递归遍历
    4. //查看每一颗树的左右深度差
    5. if(pRoot == null){
    6. return true;
    7. }
    8. int left = deep(pRoot.left);
    9. int right = deep(pRoot.right);
    10. if(left - right > 1 || left - right < -1){
    11. return false;
    12. }
    13. return IsBalanced_Solution(pRoot.left) && IsBalanced_Solution(pRoot.right);
    14. }
    15. public int deep(TreeNode root){
    16. if(root == null){
    17. return 0;
    18. }
    19. int left = deep(root.left);
    20. int right = deep(root.right);
    21. return left < right ? right + 1 : left + 1;
    22. }

    6.二叉搜索树的最近公共祖先

    二叉搜索树的最近公共祖先_牛客题霸_牛客网 (nowcoder.com)

    思路:

    先遍历找到两个目标节点,并使用列表来记录所遍历的节点.

    因为是二叉搜索树,所以遍历的思路与二维数组查找的思路很相似,当前节点的val小于目标节点时走向右节点,反之向左节点.直至找到目标节点.

    再一起遍历两个路径列表,在列表中的最后一个相同的节点就是最近的公共祖先.

     

    1. public List path(TreeNode root,int val){
    2. List ret = new ArrayList<>();
    3. while(root != null){
    4. ret.add(root.val);
    5. if(root.val == val){
    6. break;
    7. }
    8. if(root.val < val){
    9. root = root.right;
    10. }else{
    11. root = root.left;
    12. }
    13. }
    14. return ret;
    15. }
    16. public int lowestCommonAncestor (TreeNode root, int p, int q) {
    17. // write code here
    18. //前提是二叉搜索树
    19. //首先得找到两个特定的节点
    20. //并使用列表来记录路径
    21. //当当前节点比目标节点大时,走左节点.反之走右节点
    22. List path1 = path(root,p);
    23. List path2 = path(root,q);
    24. //一起遍历两个列表
    25. int len1 = path1.size();
    26. int len2 = path2.size();
    27. int ret = 0;
    28. for(int i = 0 ; i < len1 && i < len2; i++){
    29. int tmp1 = path1.get(i);
    30. int tmp2 = path2.get(i);
    31. if(tmp1 == tmp2){
    32. ret = tmp1;
    33. }else{
    34. break;
    35. }
    36. }
    37. return ret;
    38. }

    7.在二叉树中找到两个节点的最近公共祖先 

    在二叉树中找到两个节点的最近公共祖先_牛客题霸_牛客网 (nowcoder.com)

    思路:

    使用递归先来找到特定的节点.

    如果两个目标节点在同一颗小树下,则直接返回根节点.

    如果当前小树只有左节点或右节点为目标节点则返回当前的目标节点. 

    1. public int lowestCommonAncestor (TreeNode root, int o1, int o2) {
    2. // write code here
    3. if(root == null){
    4. return -1;
    5. }
    6. if(root.val == o1 || root.val == o2){
    7. return root.val;
    8. }
    9. int left = lowestCommonAncestor(root.left,o1,o2);
    10. int right = lowestCommonAncestor(root.right,o1,o2);
    11. if(left == -1){
    12. return right;
    13. }
    14. if(right == -1){
    15. return left;
    16. }
    17. return root.val;
    18. }

     

  • 相关阅读:
    五个分层维度:SpringBoot工程分层实战
    spring mvc \ spring boot \ spring cloud
    【读书笔记】《Head First设计模式(中文版)》【TBC】
    老卫带你学---Datagrip连接clickhouse
    Java日期的学习篇
    为啥大家都在用 Docker !!
    解决windows下安装python并终端运行python弹出windows商店的最终解决方案
    Nova中的api
    nasa教学纳卫星计划-36
    二、vue基础入门
  • 原文地址:https://blog.csdn.net/weixin_67719939/article/details/132974490