Leetcode Maximum Depth of Binary Tree

时间:2022-08-31 18:44:03

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.

递归求解

int maxDepth(TreeNode *root){
return root? +max(maxDepth(root->left), maxDepth(root->right)) : ;
}

非递归求解

struct Node{
TreeNode *node;
int depth;
Node(TreeNode *a = NULL , int d = ):node(a), depth(d);
}; int maxDepth1(TreeNode *root){
if(root == NULL) return ;
queue<Node> que;
Node rootNode(root,);
que.push(rootNode);
int res = ;
while(!que.empty()){
Node p = que.front();que.pop();
res = p.depth;
if(p.node->left) que.push(Node(p.node->left,p.depth+));
if(p.node->right ) que.push(Node(p.node->right,p.depth+));
}
return res;
}

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

  1. LeetCode——Maximum Depth of Binary Tree

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

  2. &lbrack;LeetCode&rsqb; 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. &lbrack;Leetcode&rsqb; 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. &lbrack;LeetCode&rsqb; Maximum Depth of Binary Tree dfs&comma;深度搜索

    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 &lpar;求树的深度&rpar;

    题意:给一棵二叉树,求其深度. 思路:递归比较简洁,先求左子树深度,再求右子树深度,比较其结果,返回: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&colon;Maximum Depth of Binary Tree【Python版】

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

  8. LeetCode&colon;Minimum Depth of Binary Tree&comma;Maximum Depth of Binary Tree

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

  9. LeetCode&colon;Maximum Depth of Binary Tree&lowbar;104

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

随机推荐

  1. 分享非常有用的Java程序 &lpar;关键代码&rpar; &lpar;三&rpar;---创建ZIP和JAR文件

    原文:分享非常有用的Java程序 (关键代码) (三)---创建ZIP和JAR文件 import java.util.zip.*; import java.io.*; public class Zip ...

  2. 怎样基于谷歌地图的Server缓存公布Image Service服务

    怎样基于谷歌地图的Server缓存公布Image Service服务 第一步:下载地图数据 下载安装水经注万能地图下载器,启动时仅仅选择电子.谷歌(这里能够依据自己的须要选择).例如以下图所看到的. ...

  3. DFS序的题目列表

    所谓dfs序就是将之前的顺序进行修改,获得一个新的序列,然后再新的序列下进行一系列其他的操作 一般题目给你的都会是一棵树,然后点之间都是无关的,我们首要的任务就是先把这些序列重新排.然后再根据dfs的 ...

  4. bzoj1096&lbrack;ZJOI2007&rsqb;仓库建设 斜率优化dp

    1096: [ZJOI2007]仓库建设 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 5482  Solved: 2448[Submit][Stat ...

  5. github配置ssh密钥的方法

    配置用户名和邮箱 初次安装git需要配置用户名和邮箱,否则git会提示:please tell me who you are. 你需要运行命令来配置你的用户名和邮箱: $ git config --g ...

  6. ELK&lpar;ElasticSearch&plus;Logstash&plus; Kibana&rpar;搭建实时日志分析平台

    一.简介 ELK 由三部分组成elasticsearch.logstash.kibana,elasticsearch是一个近似实时的搜索平台,它让你以前所未有的速度处理大数据成为可能. Elastic ...

  7. CSS--交互效果

    动画过渡效果 //vue <div class="search-list" v-show="searches.length"> <transi ...

  8. 类Shiro权限校验框架的设计和实现&lpar;2&rpar;--对复杂权限表达式的支持

    前言: 我看了下shiro好像默认不支持复杂表达式的权限校验, 它需要开发者自己去做些功能扩展的工作. 针对这个问题, 同时也会为了弥补上一篇文章提到的支持复杂表示需求, 特地尝试写一下解决方法. 本 ...

  9. MySQL&&num;160&semi;InnoDB表和索引之聚簇索引与第二索引

    MySQL InnoDB表和索引之聚簇索引与第二索引 By:授客QQ:1033553122 每个InnoDB表都有一个称之为聚簇索引(clustered index)的特殊索引,存储记录行数据.通常, ...

  10. Eclipse安卓开发环境

    首先,安卓开发就要搭建安卓开发环境,现在可能流行用AS,但是由于个对eclipse恐惧感比较小一点就选择了Eclipse: 大致流程: 1.安装java开发工具包(JDK): 2.Eclipse集成开 ...