Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱
MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina.com

领扣-104/111 二叉树的最大深度 Maximum Depth of Binary Tree MD


目录

目录
104-二叉树的最大深度 Maximum Depth of Binary Tree
问题
深度优先(递归法)
广度优先(队列)方式一
广度优先(队列)方式二
111-二叉树的最小深度 Minimum Depth of Binary Tree
题目
递归

104-二叉树的最大深度 Maximum Depth of Binary Tree

树 深度优先搜索(递归) 广度优先搜索(队列)

问题

给定一个二叉树,找出其最大深度。

二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。

说明: 叶子节点是指没有子节点的节点。

示例:

给定二叉树 [3,9,20,null,null,15,7],3/ \9  20/  \15   7

返回它的最大深度 3 。

方法声明:

class TreeNode {int val;TreeNode left;TreeNode right;TreeNode(int x) {val = x;}
}class Solution {public int maxDepth(TreeNode root) {}
}

深度优先(递归法)

class Solution {public int maxDepth(TreeNode root) {int depth = 0;if (root != null) {depth = Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;}return depth;}
}

时间复杂度:我们每个结点只访问一次,因此时间复杂度为 O(N), 其中 N 是结点的数量。
空间复杂度:在最糟糕的情况下,树是完全不平衡的,例如每个结点只剩下左子结点,递归将会被调用 N 次(树的高度),因此保持调用栈的存储将是 O(N)。但在最好的情况下(树是完全平衡的),树的高度将是 log(N)。因此,在这种情况下的空间复杂度将是 O(log(N))

广度优先(队列)方式一

广度优先搜索算法:

public static void levelTraversal(Node root) {Queue<Node> queue = new LinkedList<>(); //LinkedList是Java中最普通的一个队列queue.offer(root); //add、addLastwhile (!queue.isEmpty()) {Node node = queue.poll();//removeFirstif (node != null) {System.out.print(node.value);queue.offer(node.left);queue.offer(node.right);}}
}

修改后的算法:

class Solution {public int maxDepth(TreeNode root) {Queue<Pair<TreeNode, Integer>> queue = new LinkedList<>();queue.offer(new Pair<>(root, 0));int depth = 0;while (!queue.isEmpty()) {Pair<TreeNode, Integer> current = queue.poll();depth = Math.max(depth, current.depth); //更新 depthif (current.node != null) {queue.offer(new Pair<>(current.node.left, current.depth + 1));queue.offer(new Pair<>(current.node.right, current.depth + 1));}}return depth;}
}

定义的辅助类

class Pair<TreeNode, Integer> {TreeNode node;Integer depth;Pair(TreeNode node, Integer depth) {this.node = node;this.depth = depth;}
}

时间复杂度:O(N)
空间复杂度:O(N)

广度优先(队列)方式二

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

111-二叉树的最小深度 Minimum Depth of Binary Tree

树 深度优先搜索 递归

题目

给定一个二叉树,找出其最小深度。

最小深度是从根节点到最近叶子节点的最短路径上的节点数量。

说明: 叶子节点是指没有子节点的节点。

示例:
给定二叉树 [3,9,20,null,null,15,7],

    3/ \9  20/  \15   7

返回它的最小深度 2.

递归

class Solution {public int minDepth(TreeNode root) {if (root != null) {if (root.left == null && root.right == null) return 1; //这个判断可以省略else if (root.left == null) return minDepth(root.right) + 1;else if (root.right == null) return minDepth(root.left) + 1;else return Math.min(minDepth(root.left), minDepth(root.right)) + 1;} else return 0;}
}

2018-12-8

领扣-104/111 二叉树的最大深度 Maximum Depth of Binary Tree MD相关推荐

  1. 104.求二叉树的最大深度 Maximum Depth of Binary Tree

    求二叉树的最大深度 using System; using System.Collections.Generic; using System.Linq; using System.Text; usin ...

  2. leetcode 104. Maximum Depth of Binary Tree

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  3. 【LeetCode】104. Maximum Depth of Binary Tree (2 solutions)

    Maximum Depth of Binary Tree  Given a binary tree, find its maximum depth. The maximum depth is the ...

  4. 【LeetCode 剑指offer刷题】树题4:104 Maximum Depth of Binary Tree

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 104. Maximum Depth of Binary Tree Given a binary tree, fin ...

  5. LC 104. Maximum Depth of Binary Tree

    1.题意 104. Maximum Depth of Binary Tree Easy 98540 Given a binary tree, find its maximum depth. The m ...

  6. LeetCode - Maximum Depth of Binary Tree

    递归求二叉树的最大深度. /*** Definition for binary tree* public class TreeNode {* int val;* TreeNode left;* Tre ...

  7. LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree

    LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...

  8. Leetcode | Minimum/Maximum Depth of Binary Tree

    Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...

  9. LeetCode——Maximum Depth of Binary Tree

    LeetCode--Maximum Depth of Binary Tree Question Given a binary tree, find its maximum depth. The max ...

最新文章

  1. RDKit | RDKit 中的RECAP进行分子裂解
  2. html4与html5效果,浅谈HTML5与HTML4的10个关键区别
  3. 双轴机械臂串口控制命令开发与测试:STM32F103控制板,简易调试命令集合
  4. 监听器使用spring的bean
  5. vue中echarts 5.0版本以上不支持因为官方移除了地图数据和map文件夹
  6. 用python快速合并代码(方便软著申请)
  7. C语言圈叉游戏,圈叉棋小游戏的简单实现代码
  8. Ubuntu环境下Android反编译apk教程
  9. 光伏逆变器MPPT基本算法介绍-李星硕
  10. 字体大宝库:15款漂亮的艺术字体免费下载
  11. android设置高度比例,Android View设置宽高比
  12. 英文科技论文写作与学术报告2021秋期末考答案|网课期末考答案|学堂在线|清华大学管晓宏教授
  13. java后端实现集成支付宝APP支付(沙箱环境)
  14. 阿里云账号注册流程方法(图文教程)
  15. .dms文档打开方式
  16. uniapp中回退到上一页面并触发函数的方法
  17. 重装系统找不到固态_SSD固态硬盘才装上确找不到盘怎么办 --好文
  18. 996工作制该取消吗?
  19. 使用python下载网站视频资源
  20. 我的航拍直升机 控制基站软件的编写历程(3.5)—分析界面

热门文章

  1. 33.Linux系统介绍
  2. 《自己动手写Docker》书摘之三: Linux UnionFS
  3. ros与下位机通信常用的c++ boost串口应用
  4. mysql函数快速查找
  5. 【整理】Oracle创建/删除表空间
  6. 【技术应用】【informix】 c++版 数据库通用操作
  7. spring中间scope详细解释
  8. 全球投资者为阿里尖叫!阿里CEO张勇详解天猫商业新力量
  9. PPT | Docker定义存储-让应用无痛运行
  10. HBase的安装与使用