LeetCode——Maximum Depth of Binary Tree

时间:2022-08-31 18:39:27

LeetCode——Maximum Depth of Binary Tree

Question

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.

Answer

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/ class Solution {
public:
// 求树的高度
int maxDepth(TreeNode* root) {
if (!root)
return 0;
else {
int i = maxDepth(root->left);
int j = maxDepth(root->right);
return max(i, j) + 1;
}
}
};

LeetCode——Maximum Depth of Binary Tree的更多相关文章

  1. [LeetCode] Maximum Depth of Binary Tree 二叉树的最大深度

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

  2. Leetcode 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] Maximum depth of binary tree二叉树的最大深度

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

  4. [LeetCode] Maximum Depth of Binary Tree dfs,深度搜索

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

  5. LeetCode Maximum Depth of Binary Tree (求树的深度)

    题意:给一棵二叉树,求其深度. 思路:递归比较简洁,先求左子树深度,再求右子树深度,比较其结果,返回:max_one+1. /** * Definition for a binary tree nod ...

  6. leetcode Maximum Depth of Binary Tree python

    # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...

  7. leetcode:Maximum Depth of Binary Tree【Python版】

    # Definition for a binary tree node # class TreeNode: # def __init__(self, x): # self.val = x # self ...

  8. 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 ...

  9. LeetCode:Maximum Depth of Binary Tree_104

    LeetCode:Maximum Depth of Binary Tree [问题再现] Given a binary tree, find its maximum depth. The maximu ...

随机推荐

  1. How to convert webp to png/jpg/gif in MacOS

    Environment I'm using OS X 10.11.4 and have homebrew 1.0.5 installed. Introduction I recently downlo ...

  2. 第一次接触OOM

    前几天机器上一直遇到cpu100%,负载很高,经常报out of memory. 今天机器又遇到了,感觉这个东西无从下手,内存不够,tree看了下cache是不是太多了. 清理了一下,其实占用的不是特 ...

  3. css兼容性的问题

    https://www.douban.com/note/314793848/ 随意的一个博客ie6的兼容 这个博客比较好 http://blog.csdn.net/chuyuqing/article/ ...

  4. 嵌入式linux

    嵌入式开发 1.1开发板和宿主机的连接方法:cable 电缆可以通过 串口 网络 以及 JTGA等连接方式. JTAG:国际标准测试协议对芯片内部测试对flash烧写.注意JTAG 是一种协议,具体去 ...

  5. Hash索引和BTree索引区别

    (1)Hash 索引仅仅能满足"=","IN"和"<=>"查询,不能使用范围查询. 由于 Hash 索引比较的是进行 Hash ...

  6. Android利用Fiddler进行网络数据抓包&comma;手机抓包工具汇总

    Fiddler抓包工具 Fiddler抓包工具很好用的,它可以干嘛用呢,举个简单例子,当你浏览网页时,网页中有段视频非常好,但网站又不提供下载,用迅雷下载你又找不到下载地址,这个时候,Fiddler抓 ...

  7. MySQL installer

  8. 郑州尚学堂:如何在Java中创建对象

    作为Java开发者,每天都会创建大量的对象,但是,我们总是使用管理依赖系统(如Spring框架)来创建这些对象.其实还有其他方法可以创建对象,在接下来的文章中我会进行详细介绍. 1.使用new关键字 ...

  9. Redis笔记5-redis高可用方案

    一.哨兵机制 有了主从复制的实现以后,如果想对主服务器进行监控,那么在redis2.6以后提供了一个"哨兵"的机制.顾名思义,哨兵的含义就是监控redis系统的运行状态.可以启动多 ...

  10. 【&ast;】深入理解redis主从复制原理

    1.复制过程 从节点执行 slaveof 命令. 从节点只是保存了 slaveof 命令中主节点的信息,并没有立即发起复制. 从节点内部的定时任务发现有主节点的信息,开始使用 socket 连接主节点 ...