LeetCode系列之二叉树最大深度求解问题 C++
来源:互联网 发布:淘宝详情图片尺寸 编辑:程序博客网 时间:2024/06/06 01:53
问题: 给定二叉树, 如何计算二叉树最大深度?
算法描述如下:
如果当前节点为空, 返回0(代表此节点下方最大节点数为0)
算法描述如下:
如果当前节点为空, 返回0(代表此节点下方最大节点数为0)
如果当前节点不为空, 返回(其左子树和右子树下方最大节点数中的最大值+1)
代码如下:
/** * Definition for binary tree * 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 depth; int depthLeft; int depthRight; if(!root) { depth=0; } else{ depthLeft=maxDepth(root->left); depthRight=maxDepth(root->right); depth=1+(depthLeft > depthRight ? depthLeft : depthRight); } return depth; }};
阅读全文
0 0
- LeetCode系列之二叉树最大深度求解问题 C++
- 【leetcode Java】二叉树的递归遍历以及最大深度的求解(Java)
- 【LeetCode】104. Maximum Depth of Binary Tree 求解二叉树最大深度
- 数据结构之二叉树深度的求解
- [LeetCode] 104 二叉树最大深度
- leetcode系列(28)Maximum Depth of Binary Tree 求二叉树的最大深度
- leetcode之二叉树类之二叉树深度系列-----104/111/110/108/109 二叉树最大/最小深度/AVL树的判断和由有序序列生成(牵扯分治相关,OJ105/106)
- [leetcode]Maximum Depth of Binary Tree (求二叉树的最大深度 C语言)
- 求解二叉树的深度
- LeetCode OJ 之 Maximum Depth of Binary Tree (二叉树的最大深度)
- LeetCode 刷题之二:寻找二叉树的最大深度
- 二叉树的最大深度算法面试题-leetcode学习之旅(3)
- 二叉树最大深度
- DS之求解二叉树的叶子结点和深度
- 二叉树最大深度最小深度之微妙区别
- LeetCode系列问题求解1
- 【LeetCode】Maximum Depth of Binary Tree (二叉树最大深度)
- Leetcode Maximum Depth of Binary Tree 二叉树最大深度
- 单链表反转java代码
- ASCII表
- linux的bash环境变量
- 第三章 代码的坏味道
- IDEA 启动及简单配置
- LeetCode系列之二叉树最大深度求解问题 C++
- c++初级 之 友元函数
- 今日头条爬美女图片知识点1
- Java IO详解
- 关于切圆角的优化
- 1552:发红包1 :寻找运气王
- P1181 数列分段Section I
- 今日头条爬美女图片知识点下
- Python学习笔记(一)--变量类型