LeetCode 104. Maximum Depth of Binary Tree
来源:互联网 发布:手机申请淘宝账号 编辑:程序博客网 时间:2024/05/22 06:40
104. Maximum Depth of Binary Tree
一、问题描述
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.
二、输入输出
三、解题思路
递归
- 用递归两行代码就可以搞定。如果root为空就返回0,否则就从左子树和右字树的深度中取较大的值,然后加上一作为最终结果返回。
/** * 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 0; return 1+std::max(maxDepth(root->left), maxDepth(root->right)); }};
阅读全文
0 0
- [LeetCode]104.Maximum Depth of Binary Tree
- 【LeetCode】104.Maximum Depth of Binary Tree
- [Leetcode] 104. Maximum Depth of Binary Tree
- LeetCode --- 104. Maximum Depth of Binary Tree
- LeetCode 104. Maximum Depth of Binary Tree
- leetCode 104. Maximum Depth of Binary Tree
- 104. Maximum Depth of Binary Tree LeetCode
- 104. Maximum Depth of Binary Tree LeetCode
- LeetCode 104. Maximum Depth of Binary Tree
- leetcode:104. Maximum Depth of Binary Tree
- LeetCode 104. Maximum Depth of Binary Tree
- [LeetCode]104. Maximum Depth of Binary Tree
- leetcode 104. Maximum Depth of Binary Tree
- LeetCode 104. Maximum Depth of Binary Tree
- 【LeetCode】104. Maximum Depth of Binary Tree
- LeetCode#104. Maximum Depth of Binary Tree
- Leetcode 104. Maximum Depth of Binary Tree
- LeetCode *** 104. Maximum Depth of Binary Tree
- 融合了A~Z快速索引和侧滑菜单的ListVeiw的demo
- img图片转base64
- JVM调优浅谈
- 欢迎使用CSDN-markdown编辑器
- Mac 使用ab性能测试工具
- LeetCode 104. Maximum Depth of Binary Tree
- linux下配置两个tomcat实例
- LeetCode 107. Binary Tree Level Order Traversal II
- 脉冲云之实战Nodejs计时器的玩法
- Python 备份文件
- 时间间隔分钟数计算
- 安卓再按一次退出程序(Activity)
- MySQL学习笔记
- Spring boot jdbc 自定义SQL查询