求二叉树的最大深度

/**
* 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) {
int l,r;
if (root == NULL) return ;
l = maxDepth(root->left);
r = maxDepth(root->right);
return max(l,r)+; //一行代码的写法!!!!
//return root == NULL ? 0 : max(maxDepth(root -> left), maxDepth(root -> right)) + 1;
}
};

非递归写法1(直接摘自其他博客):

int maxDepth(TreeNode *root)
{
if (root == NULL) return ;
stack<TreeNode *> gray;
stack<int> depth;
int out = ; gray.push(root);
depth.push();
while (!gray.empty()) {
TreeNode *tmp = gray.top();
int num = depth.top();
gray.pop();
depth.pop();
if (tmp->left == NULL && tmp->right == NULL) {
out = num > out ? num : out;
}
else {
if (tmp->left != NULL) {
gray.push(tmp->left);
depth.push(num + );
}
if (tmp->right != NULL) {
gray.push(tmp->right);
depth.push(num + );
}
}
}
return out;
}

非递归写法2(直接摘自其他博客):

int maxDepth(TreeNode *root)
{
if(root == NULL)
return ; int res = ;
queue<TreeNode *> q;
q.push(root);
while(!q.empty())
{
++ res;
for(int i = , n = q.size(); i < n; ++ i)
{
TreeNode *p = q.front();
q.pop(); if(p -> left != NULL)
q.push(p -> left);
if(p -> right != NULL)
q.push(p -> right);
}
} return res;
}

【easy】104. Maximum Depth of Binary Tree 求二叉树的最大深度的更多相关文章

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

  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(二叉树最大深度) 解题思路和方法

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

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

  5. 104. Maximum Depth of Binary Tree(C++)

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

  6. LeetCode 104. Maximum Depth of Binary Tree C++ 解题报告

    104. Maximum Depth of Binary Tree -- Easy 方法 使用递归 /** * Definition for a binary tree node. * struct ...

  7. LeetCode Javascript实现 258. Add Digits 104. Maximum Depth of Binary Tree 226. Invert Binary Tree

    258. Add Digits Digit root 数根问题 /** * @param {number} num * @return {number} */ var addDigits = func ...

  8. 【LeetCode-面试算法经典-Java实现】【104-Maximum Depth of Binary Tree(二叉树的最大深度)】

    [104-Maximum Depth of Binary Tree(二叉树的最大深度)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a binary t ...

  9. leetcode 104 Maximum Depth of Binary Tree二叉树求深度

    Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question ...

随机推荐

  1. keepalived--小白博客

    一.HA集群中的相关术语 1.节点(node) 运行HA进程的一个独立主机,称为节点,节点是HA的核心组成部分,每个节点上运行着操作系统和高可用软件服务,在高可用集群中,节点有主次之分,分别称之为主节 ...

  2. 292. Nim Game(easy)

    You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...

  3. 用bisect维护一个排序的序列

    import bisect list1 = [] bisect.insort(list1, 5) bisect.insort(list1, 1) bisect.insort(list1, 3) bis ...

  4. CodeForces Round #549 Div.2

    A. The Doors 代码: #include <bits/stdc++.h> using namespace std; ; int N; , One = ; int a[maxn], ...

  5. git revert用法以及与git reset的区别

    git revert用法 git revert 撤销 某次操作,此次操作之前和之后的commit和history都会保留,并且把这次撤销 作为一次最新的提交 * git revert HEAD     ...

  6. 深入Eureka/Feign/Hystrix原理学习(1)

    第一步: 创建注册中心项目,引入cloud discovery相关依赖. ①在pom文件中引入相关依赖. ②在启动类上加上@EnableEurekaServer注解,标注这是一个注 册中心. ③在ap ...

  7. python之反射和内置函数__str__、__repr__

    一.反射 反射类中的变量 反射对象中的变量 反射模块中的变量 反射本文件中的变量 .定义:使用字符串数据类型的变量名 来获取这个变量的值 例如: name = 'xiaoming' print(nam ...

  8. java 将保单数据 生成图片

    主要代码:---------------------------------------------------------------- /** * 生成图片 * @param cellsValue ...

  9. [Qualcomm]A Detailed History of Qualcomm 高通的前世今生

    https://www.semiwiki.com/forum/content/7353-detailed-history-qualcomm.html

  10. Django模板

    Django模板系统 官方文档 常用语法 只需要记住两种特殊符号: {{  }}和 {% %} 变量相关的用{{}},逻辑相关的用{%%}. 变量 { 变量名 }} 变量名由字母数字和下划线组成. 点 ...