首页 > 其他分享 >day16

day16

时间:2023-01-30 22:34:13浏览次数:38  
标签:node return int day16 que root 节点

1、104.二叉树的最大深度

  1. 递归法

    1. 二叉树的最大深度 ==》根节点的高度

      1. 通过后序(左右中)求得根节点高度来求得二叉树最大深度。
      2. 递归三要素
        1. 确定递归函数的参数和返回值:参数就是传入树的根节点,返回就返回这棵树的深度,所以返回值为int类型。
        2. 确定终止条件:如果为空节点的话,就返回0,表示高度为0。
        3. 确定单层递归的逻辑:先求它的左子树的深度,再求右子树的深度,最后取左右深度最大的数值 再+1 (加1是因为算上当前中间节点)就是目前节点为根节点的树的深度。
    2. 代码实现

      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. 迭代法

    1. 套用二叉树层序遍历模板

    2. 代码实现

      class Solution {
          public int maxDepth(TreeNode root) {
              Queue<TreeNode> que = new LinkedList<TreeNode>();
              int depth = 0;
      
              if(root != null){
                  que.offer(root);
              }
      
              while(!que.isEmpty()){
                  int size = que.size();
                  depth++; //记录层数
      
                  for(int i=0; i<size; i++){
                      TreeNode node = que.poll();
                      if(node.left != null){
                          que.offer(node.left);
                      }
                      if(node.right != null){
                          que.offer(node.right);
                      }
                  }
              }
              return depth;
          }
      }
      

2、559.n叉树的最大深度

  1. 递归法

    1. 代码实现

      class Solution {
           public int maxDepth(Node root) {
               return getDepth(root);
           }
      
           public int getDepth(Node node){
               if(node == null){
                   return 0;
               }
      
               int depth = 0;
               for(int i=0; i<node.children.size(); i++){
                   int childDepth = getDepth(node.children.get(i));
                   depth = Math.max(childDepth, depth);
               }
      
               return depth+1; 
           }
       }
      
  2. 迭代法

    1. 代码实现

      class Solution {
          public int maxDepth(Node root) {
              if(root == null){
                  return 0;
              }
      
              Queue<Node> que = new LinkedList<Node>();
              int depth = 0;
              que.offer(root);
      
      
              while(!que.isEmpty()){
                  int size = que.size();
                  depth++;
      
                  for(int i=0; i<size; i++){
                      Node node = que.poll();
                      for(int j=0; j<node.children.size(); j++){
                          que.offer(node.children.get(j));
                      }
                  }
              }
      
              return depth;
          }
      }
      

3、111.二叉树的最小深度

  1. 注意:最小深度是从根节点到最近叶子节点的最短路径上的节点数量

    1. 叶子节点是指左右孩子节点均为空的节点
  2. 递归法

    1. 递归三部曲

      1. 参数为要传入的二叉树根节点,返回的是int类型的深度。

      2. 确定终止条件:遇到空节点返回0,表示当前节点的高度为0。

      3. 确定单层递归的逻辑:

        如果左子树为空,右子树不为空,说明最小深度是 1 + 右子树的深度。

        反之,右子树为空,左子树不为空,最小深度是 1 + 左子树的深度。

        最后如果左右子树都不为空,返回左右子树深度最小值 + 1 。

      4. 注意:遍历的顺序为后序(左右中),可以看出:求二叉树的最小深度和求二叉树的最大深度的差别主要在于处理左右孩子不为空的逻辑。

    2. 代码实现

      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;
              
          }
      }
      
  3. 迭代法

    1. 套用层序遍历模板

      class Solution {
          public int minDepth(TreeNode root) {
              Queue<TreeNode> que = new LinkedList<TreeNode>();
              int depth = 0;
      
              if(root != null){
                  que.offer(root);
              }
      
              while(!que.isEmpty()){
                  int size = que.size();
                  depth++;
      
                  for(int i=0; i<size; i++){
                      TreeNode node = que.poll();
                      if(node.right==null && node.left==null){ //遍历到叶子节点
                          return depth;
                      }
                      if(node.left != null){
                          que.offer(node.left);
                      }
                      if(node.right != null){
                          que.offer(node.right);
                      }
                  }
              }
              return depth;
          }
      }
      

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

  1. 递归法

    1. 递归三部曲

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

      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. 迭代法

    1. 套用层序遍历的模板

      class Solution {
          public int countNodes(TreeNode root) {
              if(root == null){
                  return 0;
              }
      
              Queue<TreeNode> que = new LinkedList<TreeNode>();
              int count = 0;
      
              que.offer(root);
      
              while(!que.isEmpty()){
                  int size = que.size();
                  for(int i=0; i<size; i++){
                      TreeNode node = que.poll();
                      count++; //记录节点个数
                      if(node.left != null){
                          que.offer(node.left);
                      }
                      if(node.right != null){
                          que.offer(node.right);
                      }
                  }
              }
      
              return count;
      
          }
      }
      

标签:node,return,int,day16,que,root,节点
From: https://www.cnblogs.com/hzj-bolg/p/17077439.html

相关文章

  • Day16 -闭包和装饰器
    1.闭包介绍和基本语法1.函数产生嵌套(外函数中定义一个内函数)2.内函数使用外函数定的局部变量3.外函数返回内函数的引用(函数名)闭包的介绍我们前面已经学过了......
  • day16集合
    1.Collection集合1.1数组和集合的区别【理解】相同点都是容器,可以存储多个数据不同点数组的长度是不可变的,集合的长度是可变的数组可以存基本数据类型和引......
  • 代码随想录day16 104. 二叉树的最大深度 559. N 叉树的最大深度 111. 二叉树的最小深
    104.二叉树的最大深度classSolution{public:intmaxDepth(TreeNode*root){if(root==NULL)return0;return1+max(maxDepth(root->left)......
  • javascript-代码随想录训练营day16
    104.二叉树的最大深度题目链接:https://leetcode.cn/problems/maximum-depth-of-binary-tree/题目描述:给定一个二叉树,找出其最大深度。二叉树的深度为根节点到最远叶子......
  • 代码随想录算法训练营Day16| 104. 二叉树的最大深度、559.n叉树的最大深度、111. 二叉
    代码随想录算法训练营Day16|104.二叉树的最大深度、559.n叉树的最大深度、111.二叉树的最小深度、222.完全二叉树的节点个数104.二叉树的最大深度104.二叉树的最大......
  • 剑指offer——Day16 排序(简单)
    Day162022.11.22排序(简单)45.把数组排成最小的数自己实现没有思路题解也是比较大小,只是这个比较大小的方法是两个数字字符串stringx和stringy,如果x+y<y+x,说明x应该......
  • Day16:冒泡排序详解
    冒泡排序冒泡循环有两层循环,第一层控制循环轮数,第二层循环代表元素比较的次数。利用冒泡排序获得升序或者降序的数组//利用冒泡排序将一个数组进行降序排序//思路://冒......
  • 【算法训练营day16】LeetCode104. 二叉树的最大深度 LeetCode559. n叉树的最大深度 Le
    LeetCode104.二叉树的最大深度题目链接:104.二叉树的最大深度初次尝试直接看题解学习思路。看完代码随想录后的想法本题使用的是二叉树的后序遍历,实际上是在求根节点......
  • day16-Servlet05
    Servlet0514.HttpServletRequestHttpServletRequest对象代表客户端的请求当客户端/浏览器通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中通过......
  • day16 --> (会话技术(Cookie、Session)、JSP入门)
    今日内容:1、会话技术:1.Cookie2.Sessio2、JSP入门学习 会话技术1、会话:一次会话中包含多次请求和响应。一次会话:浏览器第一次给服务器资源发送请求,会话建立,知道有一......