LeetCode 104--二叉树的最大深度
2019-03-29 21:37
357 查看
版权声明:博文千万条,版权第一条。转载不规范,博主两行泪 https://blog.csdn.net/u012135009/article/details/88902436
题目
给定一个二叉树,找出其最大深度。
二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
说明: 叶子节点是指没有子节点的节点。
示例
给定二叉树 [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 返回它的最大深度 3 。
/** * 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 height = 0; if (root != nullptr) { height++; int max_left = maxDepth(root->left); int max_right = maxDepth(root->right); height += max(max_left, max_right); } return height; } };
相关文章推荐
- [LeetCode] 104 二叉树最大深度
- LeetCode 104 Maximum Depth of Binary Tree(二叉树的最大深度)
- LeetCode:104_Maximum Depth of Binary Tree | 二叉树的最大深度 | Easy
- LeetCode:104_Maximum Depth of Binary Tree | 二叉树的最大深度 | Easy
- Leetcode 104 Maximum Depth of Binary Tree 二叉树的最大深度
- LeetCode:104_Maximum Depth of Binary Tree | 二叉树的最大深度 | 深度优先与广度优先深入理解
- leetcode之二叉树类之二叉树深度系列-----104/111/110/108/109 二叉树最大/最小深度/AVL树的判断和由有序序列生成(牵扯分治相关,OJ105/106)
- LeetCode104--二叉树最大深度
- 【LeetCode-面试算法经典-Java实现】【104-Maximum Depth of Binary Tree(二叉树的最大深度)】
- leetcode 104求二叉树的最大深度
- 【LeetCode-面试算法经典-Java实现】【104-Maximum Depth of Binary Tree(二叉树的最大深度)】
- [LeetCode-104] Maximum Depth of Binary Tree(二叉树最大深度)
- LeetCode-104.二叉树的最大深度(相关话题:递归)
- 二叉树的最大深度(leetcode-104)
- 【LeetCode】[104] 二叉树的最大深度——“递归”
- [LeetCode] Maximum Depth of Binary Tree 二叉树的最大深度
- LeetCode 刷题之二:寻找二叉树的最大深度
- [LeetCode]104. Maximum Depth of Binary Tree(二叉树最大深度)
- 二叉树的最大深度算法面试题-leetcode学习之旅(3)
- 104. 二叉树的最大深度