二叉树的深度的概念最值得注意的地方,在于 到"叶子"节点的距离。

一般来说,如果直接说“深度”,都是指最大深度,即最远叶子的距离。

这里放两道例题,最小深度和最大深度。

1. 二叉树的最小深度

Given a binary tree, find its minimum depth.

The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

  1. /**
  2. * Definition for binary tree
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  8. * };
  9. */
  10. class Solution {
  11. public:
  12. int minDepth(TreeNode *root) {
  13. }
  14. };

因为深度是必须到叶子节点的距离,因此使用深度遍历时,不能单纯的比较左右子树的递归结果返回较小值,因为对于有单个孩子为空的节点,为空的孩子会返回0,但这个节点并非叶子节点,故返回的结果是错误的。

因此,当发现当前处理的节点有单个孩子是空时,返回一个极大值INT_MAX,防止其干扰结果。

  1. class Solution {
  2. public:
  3. int minDepth(TreeNode *root) {
  4. if(!root) return ;
  5. if(!root -> left && !root -> right) return ; //Leaf means should return depth.
  6. int leftDepth = + minDepth(root -> left);
  7. leftDepth = (leftDepth == ? INT_MAX : leftDepth);
  8. int rightDepth = + minDepth(root -> right);
  9. rightDepth = (rightDepth == ? INT_MAX : rightDepth); //If only one child returns 1, means this is not leaf, it does not return depth.
  10. return min(leftDepth, rightDepth);
  11. }
  12. };

当然,这道题也能用层次遍历来做。

  1. class Solution {
  2. struct LevNode{
  3. TreeNode* Node;
  4. int Lev;
  5. };
  6. public:
  7. int minDepth(TreeNode *root) {
  8. if(NULL == root) return ;
  9. queue<LevNode> q;
  10. LevNode lnode;
  11. lnode.Node = root;
  12. lnode.Lev = ;
  13. q.push(lnode);
  14. while(!q.empty()){
  15. LevNode curNode = q.front();
  16. q.pop();
  17. if(NULL == (curNode.Node) -> left && NULL == (curNode.Node) -> right)
  18. return (curNode.Lev);
  19. if(NULL != (curNode.Node) -> left){
  20. LevNode newNode;
  21. newNode.Node = (curNode.Node) -> left;
  22. newNode.Lev = (curNode.Lev + );
  23. q.push(newNode);
  24. }
  25. if(NULL != (curNode.Node) -> right){
  26. LevNode newNode;
  27. newNode.Node = (curNode.Node) -> right;
  28. newNode.Lev = (curNode.Lev + );
  29. q.push(newNode);
  30. }
  31. }
  32. return ;
  33. }
  34. };

对于这道题,LeetCode 两种解法的时间都是 48ms

2. 二叉树的最大深度

Given a binary tree, find its maximum depth.

The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

最大深度也是到叶子节点的长度,但是因为是求最大深度,单个孩子为空的非叶子节点不会干扰到结果,因此用最简洁的处理方式就可以搞定。

  1. /**
  2. * Definition for binary tree
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  8. * };
  9. */
  10. class Solution {
  11. public:
  12. int maxDepth(TreeNode *root) {
  13. if(!root) return ;
  14. int leftDepth = maxDepth(root -> left) + ;
  15. int rightDepth = maxDepth(root -> right) + ;
  16. return max(leftDepth, rightDepth);
  17. }
  18. };

二叉树系列 - 二叉树的深度,例 [LeetCode]的更多相关文章

  1. 二叉树系列 - 二叉树里的最长路径 例 [LeetCode] Binary Tree Maximum Path Sum

    题目: Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start ...

  2. 二叉树系列 - 求两节点的最低公共祖先,例 剑指Offer 50

    前言 本篇是对二叉树系列中求最低公共祖先类题目的讨论. 题目 对于给定二叉树,输入两个树节点,求它们的最低公共祖先. 思考:这其实并不单单是一道题目,解题的过程中,要先弄清楚这棵二叉树有没有一些特殊的 ...

  3. [LeetCode系列] 二叉树最大深度求解问题(C++递归解法)

    问: 给定二叉树, 如何计算二叉树最大深度? 算法描述如下: 如果当前节点为空, 返回0(代表此节点下方最大节点数为0) 如果当前节点不为空, 返回(其左子树和右子树下方最大节点数中的最大值+1) 上 ...

  4. 树&二叉树&&满二叉树&&完全二叉树&&完满二叉树

    目录 树 二叉树 完美二叉树(又名满二叉树)(Perfect Binary Tree) 完全二叉树(Complete Binary Tree) 完满二叉树(Full Binary Tree) 树 名称 ...

  5. 二叉树&满二叉树与完全二叉树

    二叉树的定义 二叉树(Binary Tree)是n(n≥0)个元素的有限集合,该集合为空或者为由一个称为"根"的元素及两个不相交的.被分别称为左子树和右子树的二叉树组成 二叉树的基 ...

  6. 【二叉树】二叉树常用算法的C++实现

    常见算法有: 1.求二叉树的最大深度 2.求二叉树的最小深度 3.二叉树的层次遍历 4.二叉树的前序遍历 5.二叉树的中序遍历 6.二叉树的后序遍历 7.求二叉树的节点个数 8.求二叉树的叶节点个数 ...

  7. openssl之EVP系列之9---EVP_Digest系列函数的一个样例

    openssl之EVP系列之9---EVP_Digest系列函数的一个样例     ---依据openssl doc/crypto/EVP_DigestInit.pod翻译     (作者:Drago ...

  8. 数据结构(3) 第三天 栈的应用:就近匹配/中缀表达式转后缀表达式 、树/二叉树的概念、二叉树的递归与非递归遍历(DLR LDR LRD)、递归求叶子节点数目/二叉树高度/二叉树拷贝和释放

    01 上节课回顾 受限的线性表 栈和队列的链式存储其实就是链表 但是不能任意操作 所以叫受限的线性表 02 栈的应用_就近匹配 案例1就近匹配: #include <stdio.h> in ...

  9. LeetCode重建二叉树系列问题总结

    二叉树天然的递归特性,使得我们可以使用递归算法对二叉树进行遍历和重建.之前已经写过LeetCode二叉树的前序.中序.后序遍历(递归实现),那么本文将进行二叉树的重建,经过对比,会发现二者有着许多相似 ...

随机推荐

  1. Python--matplotlib 绘图可视化练手--折线图/条形图

    最近学习matplotlib绘图可视化,感觉知识点比较多,边学习边记录. 对于数据可视化,个人建议Jupyter Notebook. 1.首先导包,设置环境 import pandas as pd i ...

  2. HDU 5234 Happy birthday 01背包

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5234 bc:http://bestcoder.hdu.edu.cn/contests/con ...

  3. HDU 5464 Clarke and problem 动态规划

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5464 Clarke and problem  Accepts: 130  Submissions: ...

  4. ranch代码简述

    最近要看一下erlang连接池,觉得ranch很不错. github上面有人写了ranch的代码阅读,可以看一下,链接在这里. 1. ranch可以同时监听多个端口,每个端口的连接信息可以单独配置. ...

  5. .NET Core 控制台中文乱码问题!

    class Program { static void Main(string[] args) { Encoding.RegisterProvider(CodePagesEncodingProvide ...

  6. Docker 技术 介绍

    https://github.com/docker/docker 实现用户空间隔离的技术:名称空间(NameSpace),CGroup(控制组) 什么是NameSpace::简单的理解就是,每一个虚拟 ...

  7. jQuery之属性

    1. 操作任意属性 attr() 操作非布尔值的 removeAttr() prop() 操作布尔值的2. 操作class属性 addClass() 添加class属性 removeClass() 移 ...

  8. ViewController 视图控制器的常用方法

    ViewController 视图控制器 ,是控制界面的控制器,通俗的来说,就是管理我们界面的大boss,视图控制器里面包含了视图,下面举个例子,让视图在两个视图上调转. 定义一个视图控制器: MyV ...

  9. LoadRunner函数大全之中文解释

    LoadRunner函数大全之中文解释

  10. delphi 中如何执行SqlParameter形式的SQL语句

    procedure TForm1.Button1Click(Sender: TObject); begin ADOConnection1.Open('); ADOQuery1.Close; ADOQu ...