[DFS] leetcode 104. Maximum Depth of Binary Tree
2017-06-26 20:50
197 查看
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.
/**
* 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;
int l=maxDepth(root->left);
int r=maxDepth(root->right);
return l>r?1+l:1+r;
}
};
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
/**
* 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;
int l=maxDepth(root->left);
int r=maxDepth(root->right);
return l>r?1+l:1+r;
}
};
相关文章推荐
- [leetcode][list][dfs] Convert Sorted List to Binary Search Tree
- LeetCode 606.[DFS] Construct String from Binary Tree
- 【leetcode】104. Maximum Depth of Binary Tree【java】三种实现方法:递归、BFS、DFS
- leetcode 104 Maximum Depth of Binary Tree(DFS)
- leetcode 110. Balanced Binary Tree DFS
- Leetcode bfs&dfs Binary Tree Postorder Traversal II
- leetcode 110. Balanced Binary Tree(平衡二叉树的判断) DFS深度优先遍历
- Leetcode - Tree - 104. Maximum Depth of Binary Tree(DFS求二叉树最深深度)
- leetcode 235. Lowest Common Ancestor of a Binary Search Tree 二叉搜索树BST的最近公共祖先LCA + 深度优先遍历DFS
- leetcode dfs Validate Binary Search Tree
- [leetcode][tree][dfs] Balanced Binary Tree
- Leetcode dfs Construct Binary Tree from Preorder and Inorder Traversal
- 【LeetCode】111. Minimum Depth of Binary Tree解法及注释,Java,C++,DFS
- leetcode 236. Lowest Common Ancestor of a Binary Tree 最近公告祖先LCA + 二叉树 + 深度优先遍历DFS
- LeetCode 111 Minimum Depth of Binary Tree(DFS)
- leetcode 98. Validate Binary Search Tree DFS深度优先搜索 + 两个递归函数 + 一个错误做法
- leetcode 543. Diameter of Binary Tree 最长树的片段 + 深度优先遍历DFS
- 【LeetCode】104. Maximum Depth of Binary Tree(DFS|BFS)
- LeetCode@DFS_257_Binary_Tree_Paths
- [leetcode][tree][dfs] Minimum Depth of Binary Tree