【问题】给定一个二叉树,找出其最大深度。

二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
说明: 叶子节点是指没有子节点的节点。

示例:
给定二叉树 [3,9,20,null,null,15,7],

/ \

/ \
7 返回它的最大深度 3 。

【DFS解法】

我们使用栈结构来储存每个节点root以及该节点的深度deep,由于对tuple的使用还不太熟练,需要多练习,一次使用tuple来讲树结构体指针和对应的整型变量深度。从根节点开始遍历,首先一直遍历左子节点,并将节点压入栈中,如果左子节点为空,则从栈中弹出,并开始遍历弹出节点的右子节点。这个过程也就相当于回溯了,回到上一级去。

**
* 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 == nullptr) return ;
stack<tuple<TreeNode*, int>> sta;
int maxdeep = ;
int deep = ;
while(!sta.empty() || root){
while(root){
sta.push(make_tuple(root, deep+));
deep++;
root = root->left;
}
tie(root, deep) = sta.top();
if(maxdeep < deep) maxdeep = deep;
sta.pop();
root = root->right;
}
return maxdeep;
}
};

【BFS解法】

这个其实质就是层次遍历,使用队列来储存树节点,并使用size变量记录每次节点的数量,在一次循环中,处理掉一层的节点,具体做法:将某一层所有节点的子节点压入队列后,并将所有的节点移除队列。
在处理某一层的树节点的同时,使用count变量记录处理的层数。即为最大深度!

/**
* 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 == nullptr){
return ;
}
queue<TreeNode*> que;
que.push(root);
int count = ;
while(que.size() != ){
int size = que.size(); while(size--){
TreeNode* tmp = que.front();
if(tmp->left != nullptr) que.push(tmp->left);
if(tmp->right != nullptr) que.push(tmp->right);
que.pop();
}
count++;
}
return count;
}
};

【LeetCode】二叉树的最大深度的更多相关文章

  1. LeetCode - 二叉树的最大深度

    自己解法,欢迎拍砖 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例:给定二叉树 [3,9,20,null,nu ...

  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初级算法--树01:二叉树的最大深度

    LeetCode初级算法--树01:二叉树的最大深度 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.n ...

  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. LeetCode 104. 二叉树的最大深度(Maximum Depth of Binary Tree)

    104. 二叉树的最大深度 104. Maximum Depth of Binary Tree 题目描述 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说 ...

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

  7. LeetCode(104):二叉树的最大深度

    Easy! 题目描述: 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例:给定二叉树 [3,9,20,null, ...

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

  9. LeetCode 腾讯精选50题--二叉树的最大深度

    求二叉树的最大深度, 基本思路如下: 设定一个全局变量记录二叉树的深度,利用递归,没遍历一层都将临时深度变量+1,并在每一节点递归结束后判断深度大小. 具体代码如下: package algorith ...

  10. Java实现 LeetCode 104 二叉树的最大深度

    104. 二叉树的最大深度 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例: 给定二叉树 [3,9,20,nu ...

随机推荐

  1. 700k把web端程序包装为桌面程序

    electron因为自带cef所以体积巨大,还不是因为windows没有chromium的webview嘛,现在有了新edge后,这个项目通过依赖各个平台的webview,并依赖.net core,做 ...

  2. Spark 读 Hive(不在一个 yarn 集群)

    方法一 1. 找到目标 Hive 的 hive-site.xml 文件,拷贝到 spark 的 conf 下面. 在我的情况下 /etc/hive/conf/hive-site.xml -> / ...

  3. js动态添加li,你添加的li具有你绑定的事件

    2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 <%@page i ...

  4. Day 1:思考

    干游戏这行从实习到工作算起来也有快7年的时间了, 7年的时间~上学了.毕业了.工作了.结婚了.孩子要出生了~ 也算是经历了不少的事情了,自己觉得生活过的是越来越好了, 自己做过的游戏也不算少了,不过真 ...

  5. Python中的*可变参数与**关键字参数

    1.定义了一个需要两个参数的函数 def print_str(first, second): print first print second if __name__ == "__main_ ...

  6. POJ - 1847 Tram(dijkstra)

    题意:有向图有N个点,当电车进入交叉口(某点)时,它只能在开关指向的方向离开. 如果驾驶员想要采取其他方式,他/她必须手动更换开关.当驾驶员从路口A驶向路口B时,他/她尝试选择将他/她不得不手动更换开 ...

  7. s2010编译C++ 链栈的使用

    // CTest.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #include &l ...

  8. SpringCloud实战——(1)创建SpringCloud项目

    首先创建一个SpirngCloud工程,并添加公用依赖. <?xml version="1.0" encoding="UTF-8"?> <pr ...

  9. 2. react 简书 头部(header) 图标添加

    1. 访问 iconfont 并注册 登陆 2. 进入 iconfont 头部 图标管理->我的项目 3. 点击右边的文件夹 + 号 图标 创建我的项目 输入项目名称即可 4.在 搜索框 搜索 ...

  10. Day7 - E - Strange Way to Express Integers POJ - 2891

    Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative ...