LeetCode #104 - Maximum Depth of Binary Tree - Easy
2016-11-07 21:40
417 查看
Problem
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.
Example
1 / \ 2 2 \ \ 3 3 maximum depth = 3
Algorithm
整理一下题意:给定一个二叉树,要求返回其最大深度本题比较简单,直接通过DFS,每次返回子树的最大深度加一即可得到。代码如下。
//DFS, /** * 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==NULL) return 0; return 1+max(maxDepth(root->left),maxDepth(root->right)); } };
相关文章推荐
- LeetCode:104_Maximum Depth of Binary Tree | 二叉树的最大深度 | Easy
- 【Leetcode-easy-104】Maximum Depth of Binary Tree
- [Leetcode 104, Easy] Minimum (Maximum) Depth of Binary Tree
- LeetCode:104_Maximum Depth of Binary Tree | 二叉树的最大深度 | Easy
- 【leetcode_easy】104-MaximumDepthofBinaryTree
- leetcode 104_MaximumDepthOfBinaryTree_easy.py
- 【LeetCode】(104)Maximum Depth of Binary Tree(Easy)
- leetcode[104]:Maximum Depth of Binary Tree
- [Leetcode 8] 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】【Easy】Maximum Depth of Binary Tree
- Leetcode_104_Maximum Depth of Binary Tree
- [leetcode 104] Maximum Depth of Binary Tree