代码随想录算法训练营第16天|● 104.二叉树的最大深度 559.n叉树的最大深度 ● 111.二叉树的最小深度 ● 222.完全二叉树的节点个数

104. 二叉树的最大深度

简单
给定一个二叉树 root ,返回其最大深度。
二叉树的 最大深度 是指从根节点到最远叶子节点的最长路径上的节点数。

示例 1:
[图片]

输入:root = [3,9,20,null,null,15,7]
输出:3
示例 2:
输入:root = [1,null,2]
输出:2

提示:

  • 树中节点的数量在 [0, 10(4)] 区间内。
  • -100 <= Node.val <= 100

代码

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func max(a, b int) int {
  if a > b {
    return a
  }
  return b
}

// 递归
func maxDepth(root *TreeNode) int {
  if root == nil {
    return 0
  }
  return max(maxdepth(root.Left), maxdepth(root.Right)) + 1
}

//层次遍历
func maxDepth(root *TreeNode) int {
    queue := make([]*TreeNode, 0)
    res := make([][]int, 0)
    queue = append(queue, root)
    for len(queue) > 0 {
        n := len(queue)
        next, r := make([]*TreeNode, 0), make([]int, 0)
        for i := 0; i < n; i++ {
            x := queue[i]
            if x != nil {
                next = append(next, x.Left, x.Right)
                r = append(r, x.Val)
            }
        }
        if len(r) > 0 {
            res = append(res, r)
        }

        queue = next
    }
    return len(res)
}

559. N 叉树的最大深度

简单
给定一个 N 叉树,找到其最大深度。
最大深度是指从根节点到最远叶子节点的最长路径上的节点总数。
N 叉树输入按层序遍历序列化表示,每组子节点由空值分隔(请参见示例)。

示例 1:
[图片]
输入:root = [1,null,3,2,4,null,5,6]
输出:3
示例 2:
[图片]
输入:root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
输出:5

提示:

  • 树的深度不会超过 1000 。
  • 树的节点数目位于 [0, 10(4)] 之间。

代码

/**
 * Definition for a Node.
 * type Node struct {
 *     Val int
 *     Children []*Node
 * }
 */

func maxDepth(root *Node) int {
     if root == nil {
          return 0
     }
     cnt := 0
     for _, nd := range root.Children {
          n := maxDepth(nd)
          if cnt < n {
               cnt = n
          }
     }
     return cnt + 1
}

111. 二叉树的最小深度

简单
给定一个二叉树,找出其最小深度。
最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
说明:叶子节点是指没有子节点的节点。

示例 1:
[图片]
输入:root = [3,9,20,null,null,15,7]
输出:2
示例 2:
输入:root = [2,null,3,null,4,null,5,null,6]
输出:5

提示:

  • 树中节点数的范围在 [0, 10(5)] 内
  • -1000 <= Node.val <= 1000

思路

有误区,需要区分根节点,少一边子树的情况
[图片]

代码

func minDepth(root *TreeNode) int {
    if root == nil {
        return 0
    }

    if root.Left == nil && root.Right == nil {
        return 1
    }

    l := minDepth(root.Left)
    r := minDepth(root.Right)

    if root.Left == nil || root.Right == nil {// 当一个左子树为空,右不为空,这时并不是最低点
        return l + r + 1//检查左子树或右子树是否为零。 如果其中之一为nil,我们返回非nil子树的深度加1。
    }

    return min(l, r) + 1
}

func min(a, b int) int {
    if a < b {
        return a
    }
    return b
}

package __tree

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func minDepth(root *TreeNode) int {
   if root == nil {
      return 0
   }

   queue := make([]*TreeNode, 0)
   queue = append(queue, root)
   depth := 1

   for len(queue) > 0 {
      n := len(queue)
      for i := 0; i < n; i++ {
         node := queue[i]
         if node.Left == nil && node.Right == nil { //找到叶子节点
            return depth
         }
         if node.Left != nil {
            queue = append(queue, node.Left)
         }
         if node.Right != nil {
            queue = append(queue, node.Right)
         }
      }
      queue = queue[n:]
      depth++
   }

   return depth
}

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

中等
给你一棵 完全二叉树 的根节点 root ,求出该树的节点个数。
完全二叉树 的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第 h 层,则该层包含 1~ 2(h) 个节点。

示例 1:
[图片]
输入:root = [1,2,3,4,5,6]
输出:6
示例 2:
输入:root = []
输出:0
示例 3:
输入:root = [1]
输出:1

提示:

  • 树中节点的数目范围是[0, 5 * 10(4)]
  • 0 <= Node.val <= 5 * 10(4)
  • 题目数据保证输入的树是 完全二叉树

进阶:遍历树来统计节点是一种时间复杂度为 O(n) 的简单解决方案。你可以设计一个更快的算法吗?

代码

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func countNodes(root *TreeNode) int {
  if root==nil{
    return 0
  }
  return countNodes(root.Left) + countNodes(root.Right) +1
}

你可能感兴趣的:(代码随想录,算法,数据结构)