DFS:104. Maximum Depth of Binary Tree
2017-09-23 20:24
204 查看
最大深度,典型的dfs。差点又想加一个if(root->left == NULL && root->right == NULL),没必要。代码很简单:
/**
* 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;
else
return max(maxDepth(root->left), maxDepth(root->right)) + 1;
}
};
相关文章推荐
- LeetCode oj 104. Maximum Depth of Binary Tree(DFS||BFS)
- Construct Binary Tree from Preorder and Inorder Traversal (DFS,参考)
- DFS Validate Binary Search Tree
- 110. Balanced Binary Tree (Tree; DFS)
- LeetCode-Binary Tree Maximum Path Sum-二叉树最大路径和-DFS
- leetcode dfs Minimum Depth of Binary Tree
- leetcode 110. Balanced Binary Tree DFS
- (M)DFS:98. Validate Binary Search Tree
- [LeetCode] Minimum Depth of Binary Tree - DFS
- leetcode 662. Maximum Width of Binary Tree 二叉树最大宽度 + 深度优先遍历DFS
- L3:06 - Binary Tree DFS Template
- 【DFS&二叉树】Binary Tree Maximum Path Sum
- [leetcode][dfs] Lowest Common Ancestor of a Binary Search Tree
- LeetCode@DFS_257_Binary_Tree_Paths
- (M)DFS:109. Convert Sorted List to Binary Search Tree
- LeetCode 606.[DFS] Construct String from Binary Tree
- LeetCode 111 Minimum Depth of Binary Tree(二叉树的最短深度)(BT、DFS)(*)
- dfs-104. Maximum Depth of Binary Tree
- Posts Tagged 【list && tree && dfs】Flatten Binary Tree to Linked List
- Flatten Binary Tree to Linked List (DFS)