首页 > 其他分享 >day3

day3

时间:2023-03-29 21:13:27浏览次数:25  
标签:node return int day3 queue root public

1、104 二叉树的最大深度 559 n叉树的最大深度

  1. 104 二叉树的最大深度

    1. 递归法

      1. 本题可以使用前序(中左右),也可以使用后序遍历(左右中),使用前序求的就是深度,使用后序求的是高度。

      2. 根节点的高度就是二叉树的最大深度,本题中通过后序求的根节点高度来求的二叉树最大深度。

      3. 代码

        class Solution {
            public int maxDepth(TreeNode root) {
                return getDepth(root);
            }
        
            public int getDepth(TreeNode node) {
                if(node == null) {
                    return 0;
                }
        
                int leftDepth = getDepth(node.left);
                int rightDepth = getDepth(node.right);
                int depth = Math.max(leftDepth, rightDepth) + 1;
        
                return depth;
        
            }
        }
        
    2. 迭代(层序遍历)

      class Solution {
          public int maxDepth(TreeNode root) {
              int depth = 0;
              Deque<TreeNode> queue = new ArrayDeque<>();
              if(root!=null) {
                  queue.offerLast(root);
              }
      
              while(!queue.isEmpty()) {
                  depth ++;
                  int size = queue.size();
      
                  for(int i=0; i<size; i++ ) {
                      TreeNode node = queue.pollFirst();
                      if(node.left != null) {
                          queue.offerLast(node.left);
                      }
                      if(node.right != null) {
                          queue.offerLast(node.right);
                      }
                  }
              }
      
              return depth;
      
          }
      }
      
  2. 599 n叉树的最大深度

    1. 递归法

      class Solution {
          public int maxDepth(Node root) {
              return getHeight(root);
          }
      
          public int getHeight(Node node) {
              if(node == null) {
                  return 0;
              }
              
              int maxChildHeight = 0;
              for(int i=0; i<node.children.size(); i++) {
                  int childHeight = getHeight(node.children.get(i));
                  maxChildHeight = Math.max(maxChildHeight,childHeight);
              }
              int height = maxChildHeight+1;
              return height;
          }
      }
      
    2. 迭代法(层序遍历)

      class Solution {
          public int maxDepth(Node root) {
              int depth = 0;
              Deque<Node> queue = new ArrayDeque<>();
      
              if(root!=null) {
                  queue.offerLast(root);
              }
      
              while(!queue.isEmpty()) {
                  int size = queue.size();
                  depth++;
      
                  for(int i=0; i<size; i++) {
                      Node node = queue.pollFirst();
                      for(int j=0; j<node.children.size(); j++) {
                          queue.offerLast(node.children.get(j));
                      }
                  }
              }
              
              return depth;
          }
      }
      

2、111 二叉树的最小深度

  1. 递归法

    1. 如果左子树为空,右子树不为空,说明最小深度是 1 + 右子树的深度。
    2. 反之,右子树为空,左子树不为空,最小深度是 1 + 左子树的深度。
    3. 最后如果左右子树都不为空,返回左右子树深度最小值 + 1 。
    class Solution {
        public int minDepth(TreeNode root) {
            return getDepth(root);
        }
    
        public int getDepth(TreeNode node) {
            if(node == null){
                return 0;
            }
    
            int leftDepth = getDepth(node.left);
            int rightDepth = getDepth(node.right);
    
            if(node.left == null && node.right != null){
                return rightDepth + 1;
            }
            if(node.left != null && node.right == null){
                return leftDepth + 1;
            }
    
            int depth = 1 + Math.min(leftDepth, rightDepth);
            return depth;
            
        }
    }
    
    
  2. 迭代法(层序遍历)

    • 只有当左右孩子都为空的时候,才说明遍历到最低点了。如果其中一个孩子不为空则不是最低点
    class Solution {
        public int minDepth(TreeNode root) {
            int depth = 0;
            Deque<TreeNode> queue = new ArrayDeque<>();
            if(root!=null) {
                queue.offerLast(root);
            }
    
            while(!queue.isEmpty()) {
                int size = queue.size();
                depth++;
                for(int i=0; i<size; i++) {
                    TreeNode node = queue.pollFirst();
                    if(node.left ==null && node.right == null) {
                        return depth;
                    } 
                    if(node.left!=null) {
                        queue.offerLast(node.left);
                    }
                    if(node.right!=null) {
                        queue.offerLast(node.right);
                    }
                }
            }
    
            return depth;
        }
    }
    

3、222 完全二叉树的节点个数

  1. 递归法

    • 类似于 104 二叉树的最大高度

    • 递归遍历顺序:从下往上 ==》 后序遍历:左右中

    • 递归三部曲

      • 确定递归函数的参数和返回值:参数就是传入树的根节点,返回就返回以该节点为根节点二叉树的节点数量,所以返回值为int类型。
      • 确定终止条件:如果为空节点的话,就返回0,表示节点数为0。
      • 确定单层递归的逻辑:先求它的左子树的节点数量,再求右子树的节点数量,最后取总和再加一 (加1是因为算上当前中间节点)就是目前节点为根节点的节点数量。
    • 代码

      class Solution {
          public int countNodes(TreeNode root) {
              return getCount(root);
          }
      
          public int getCount(TreeNode node) {
              if(node == null) {
                  return 0;
              }
      
              int leftCount = getCount(node.left);
              int rightCount = getCount(node.right);
              int count = leftCount + rightCount + 1;
      
              return count;
          }
      }
      
  2. 迭代法(层序遍历)

    class Solution {
        public int countNodes(TreeNode root) {
            int count = 0;
            Deque<TreeNode> queue = new ArrayDeque<>();
            if(root != null) {
                queue.offerLast(root);
            }
    
            while(!queue.isEmpty()) {
                int size = queue.size();
                for(int i=0; i<size; i++) {
                    TreeNode node = queue.pollFirst();
                    count++;
                    if(node.left != null) {
                        queue.offerLast(node.left);
                    }
                    if(node.right != null) {
                        queue.offerLast(node.right);
                    }
                }
            }
    
            return count;
        }
    }
    

标签:node,return,int,day3,queue,root,public
From: https://www.cnblogs.com/hzj-bolg/p/17270323.html

相关文章

  • day35
    力扣题目链接(opensnewwindow)在柠檬水摊上,每一杯柠檬水的售价为 5 美元。顾客排队购买你的产品,(按账单bills支付的顺序)一次购买一杯。每位顾客只买一杯柠檬水,然后......
  • day34
    力扣题目链接(opensnewwindow)给定一个整数数组A,我们只能用以下方法修改该数组:我们选择某个索引i 并将A[i]替换为-A[i],然后总共重复这个过程K次。(我们可以多次......
  • 决战圣地玛丽乔亚Day39 -----GC、内存模型、类加载
    内存模型:java内存模型定义了JVM虚拟机如何与计算机的内存进行交互。java内存模型把内存划分为两部分:主内存和工作内存。主内存共享,工作内存线程私有。java内存模型的实现......
  • day3-1 dos命令
    打开cmd的方式开始+系统+命令提示符win+R输入cmd在任意文件夹下面,按住shift+鼠标右键点击,在此处打开命令行窗口在资源管理器地址栏前面加cmd管理员身份运......
  • day32
    力扣题目链接(opensnewwindow)给定一个数组,它的第 i个元素是一支给定股票第i天的价格。设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易(多......
  • day31
    力扣题目链接(opensnewwindow)假设你是一位很棒的家长,想要给你的孩子们一些小饼干。但是,每个孩子最多只能给一块饼干。对每个孩子i,都有一个胃口值 g[i],这是能让孩子......
  • day3 | 203. 移除链表元素,206. 反转链表,707. 设计链表
    203.移除链表元素 题目描述 给你一个链表的头节点head和一个整数val,删除链表中的那些存储的值为val的节点,并且返回新的头节点。 思路: 1.创建一个虚拟头节点,......
  • 算法随想Day39【动态规划】| LC518-零钱兑换Ⅱ、LC37-组合总和Ⅳ
    LC518.零钱兑换Ⅱ刚开始一气写成,没想到犯了个那么傻×的毛病classSolution{public: intchange(intamount,vector<int>&coins){intsize=coins.......
  • 算法随想Day36【动态规划】| LC343-整数拆分、LC96-不同的二叉搜索树
    动态规划五部曲确定dp[i]的含义dp递推公式dp数组如何初始化确认dp数组遍历顺序打印dp数组,主要用于调试LC343.整数拆分dp[i]含义:数字i被拆解后的最大乘积递推......
  • 算法随想Day37【动态规划】| LC416-分割等和子集
    动态规划五部曲确定dp[i]的含义dp递推公式dp数组如何初始化确认dp数组遍历顺序打印dp数组,主要用于调试LC416.分割等和子集这道题是“背包问题”的应用,但其实不好......