LeetCode 104. Maximum Depth of Binary Tree
2017-03-22 17:35
295 查看
104. Maximum Depth of Binary Tree
Description
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.
Analysis
这道题的意思是找到二叉树的最大深度。
我的做法就是分别计算左子树跟右子数的最大深度,返回两者之间的较大值。
将较大值加一则为这个二叉树的最大深度。
注意二叉树为空树的情况。
Code
Description
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.
Analysis
这道题的意思是找到二叉树的最大深度。
我的做法就是分别计算左子树跟右子数的最大深度,返回两者之间的较大值。
将较大值加一则为这个二叉树的最大深度。
注意二叉树为空树的情况。
Code
/** * 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 res; if(root == NULL) return 0; else return 1+max(maxDepth(root->right),maxDepth(root->left)); } };
相关文章推荐
- <LeetCode OJ> Invert Binary Tree【226】
- leetcode解题报告:104. Maximum Depth of Binary Tree
- LeetCode543. Diameter of Binary Tree
- leetcode 654. Maximum Binary Tree
- LeetCode——Binary Tree Inorder Traversal
- 【LeetCode】111.Minimum Depth of Binary Tree
- [Leetcode] Binary tree -- 501. Find Mode in Binary Search Tree
- LeetCode-104. Maximum Depth of Binary Tree
- 第十八周:[Leetcode]111. Minimum Depth of Binary Tree
- LeetCode 226. Invert Binary Tree (反转二叉树)
- [Leetcode] Binary tree--653. Two Sum IV
- leetcode记录 104. Maximum Depth of Binary Tree
- LeetCode *** 111. Minimum Depth of Binary Tree
- LeetCode 111. Minimum Depth of Binary Tree
- [Leetcode] Binary tree-- 637. Average of Levels in Binary Tree
- [LeetCode] 111. Minimum Depth of Binary Tree
- [leetcode: Python]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