LeetCode二叉树的最大深度C++版
2019-06-01 22:06
295 查看
题目描述:
给定一个二叉树,找出其最大深度。
二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
说明: 叶子节点是指没有子节点的节点。
示例:
给定二叉树 [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7
返回它的最大深度 3 。
C++代码如下:
/** * 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 left,right; if (root == NULL){ return 0; } left = maxDepth(root->left); right = maxDepth(root->right); return max(left,right)+1; } };
相关文章推荐
- [LeetCode系列] 二叉树最大深度求解问题(C++递归解法)
- C++Leetcode559:N叉树的最大深度
- [Java代码] [Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度
- leetcode 104. Maximum Depth of Binary Tree (求二叉树的最大深度,DFS,BFS)
- leetcode 二叉树的最大深度
- [Leetcode] 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刷题:二叉树的最大深度(day15)
- leetcode:654最大二叉树(C++)
- 二叉树的最大深度(leetcode-104)
- LeetCode 刷题之二:寻找二叉树的最大深度
- leetcode 104求二叉树的最大深度
- 【LeetCode】 Maximum Depth of Binary Tree 二叉树的最大深度
- 【LeetCode-面试算法经典-Java实现】【104-Maximum Depth of Binary Tree(二叉树的最大深度)】
- Leetcode 104 python 二叉树的最大深度
- [LeetCode] Maximum Depth of Binary Tree 二叉树的最大深度
- LeetCode 104 二叉树的最大深度
- 《LeetCode》104Maximum Depth of Binary Tree(二叉树的最大深度)