要求:求二叉树的深度(二叉树的深度为最远叶子节点到根节点的距离,即根节点到最远叶子节点的距离)

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.

有两种求解的思路,一种采用DFS的思想,一种采用BFS的思想,如下代码所示:

 struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x): val(x), left(NULL),right(NULL) {}
}; //采用DFS的思想
int maxDepth(TreeNode *root)
{
if (NULL == root)
return ;
int l = maxDepth(root->left);
int r = maxDepth(root->right); return l > r ? l + :r+;
//以上这两种方式有一种更简便的方法
//return 1 + max(maxDepth(root->left), maxDepth(root->right));
} //采用BFS的方法,引入队列
int maxDepth(TreeNode *root)
{
if (NULL == root)
return ;
queue <TreeNode *> que;
int nCount = ;
int nDepth = ;// 记录队列里面每一层上的元素 que.push(root);
while(!que.empty()) {
TreeNode *pTemp = que.front();
que.pop();
nCount --; if (pTemp->left)
que.push(pTemp->left);
if (pTemp->right)
que.push(pTemp->right); if (nCount == ) {
nDepth ++;
nCount = que.size();
}
}
return nDepth;
}

LeetCode:104_Maximum Depth of Binary Tree | 二叉树的最大深度 | Easy的更多相关文章

  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] Minimum Depth of Binary Tree 二叉树的最小深度

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

  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. [LintCode] Maximum Depth of Binary Tree 二叉树的最大深度

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

  6. [LeetCode] Minimum Depth of Binary Tree 二叉树最小深度

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

  7. 【LeetCode】Maximum Depth of Binary Tree(二叉树的最大深度)

    这道题是LeetCode里的第104道题. 给出题目: 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例: 给定 ...

  8. LeetCode 104. Maximum Depth of Binary Tree二叉树的最大深度 C++/Java

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

  9. [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 l ...

随机推荐

  1. 写出良好风格的JS、CSS代码

    现在代码的格式都有 eslint.prettier.babel 这些来保证,但是技术手段再高端都不能解决代码可读性的问题. 因为这个只有个人才能解决.但是注意一下事项,可以显著提高代码的可读性.可识别 ...

  2. Anaconda3(python3.5.2)中安装opencv3

    1 安装Visual C++2015 redistributable 我是64位和32的都安装了,如果你电脑中已经安装了17的,就先卸载了,不然安装不上. 2 安装依赖包Numpy.Scipy Num ...

  3. yii2 basic版本的一些配置

    1.nginx配置 重写规则 修改访问模式为 http://wh.store/admin/index 文件位置: /home/wwwroot/default/yii2-app-basic/config ...

  4. 好看的alert弹出框sweetalert

    转载:https://www.cnblogs.com/lamp01/p/7215408.html

  5. git tag的用法

    我们常常在代码封板时,使用git 创建一个tag ,这样一个不可修改的历史代码版本就像被我们封存起来一样,不论是运维发布拉取,或者以后的代码版本管理,都是十分方便的 git的tag功能 git 下打标 ...

  6. 20175234 2018-2019-2 《Java程序设计》第四周学习总结

    20175234 2018-2019-2 <Java程序设计>第四周学习总结 教材学习内容总结 教材学习了子类,其重点是方法重写.对象的上转型对象和多态,强调了面向抽象编程的思想. 学习I ...

  7. findViewById(R.id.btn_first) 给写成 R.layout.

    窗体内放了个按钮, findViewById(R.id.btn_first) 给写成 R.layout. 在java 里边引用结果就是找不到那个id 找了半天找不到原因, 奔着网上常见R找不到的问题, ...

  8. ArrayList 初探

    1.ArrayList继承AbstractList,实现List.RandomAccess.Cloneable.Serializable接口 public class ArrayList<E&g ...

  9. SpringMVC学习 十三 拦截器栈

    拦截器栈:就是有多个拦截器同时拦截相同的控制器(controller)请求,这写拦截器就构成了拦截器栈. 栈的特点是先进后出,在拦截器栈中也是如此,如果先执行了preHandle方法,也就是意味着先进 ...

  10. 【Selenium】【BugList2】geckodriver未安装,报:WebDriverException: Message: 'geckodriver' executable needs to be in PATH.

    环境信息:Windows7 64位 + python 3.6.5 + selenium 3.11.0 +pyCharm #coding=utf-8 from selenium import webdr ...