[leetcode]Maximum Depth of Binary Tree
2015-03-18 12:57
288 查看
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.
DFS
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
DFS
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: int maxD(TreeNode *root,int depth){ if(root->left==NULL && root->right == NULL) return depth; else if(root->left!=NULL && root->right == NULL) return maxD(root->left,depth+1); else if(root->left==NULL && root->right != NULL) return maxD(root->right,depth+1); else return max(maxD(root->left,depth+1),maxD(root->right,depth+1)); } int maxDepth(TreeNode *root) { if(root == NULL) return 0; return maxD(root,1); } };
相关文章推荐
- [leetcode]Maximum Depth of Binary Tree
- LeetCode Maximum Depth of Binary Tree
- Maximum Depth of Binary Tree 【leetCode】
- [LeetCode-104] Maximum Depth of Binary Tree(二叉树最大深度)
- 【LeetCode】Maximum Depth of Binary Tree
- leetcode 刷题之路 62 Maximum Depth of Binary Tree
- leetcode 104: Maximum Depth of Binary Tree
- [LeetCode]Maximum Depth of Binary Tree
- leetcode[104]:Maximum Depth of Binary Tree
- LeetCode 104----Maximum Depth of Binary Tree
- LeetCode-Maximum/Minimum Depth of Binary Tree
- [leetcode]Maximum Depth of Binary Tree
- LeetCode:Maximum Depth of Binary Tree
- 【LeetCode】Maximum Depth of Binary Tree 解题报告
- leetcode [Maximum Depth of Binary Tree]
- LeetCode 104 -Maximum Depth of Binary Tree ( JAVA )
- Leetcode-Maximum Depth of Binary Tree
- [leetcode]21 Maximum Depth of Binary Tree
- leetcode 24: Maximum Depth of Binary Tree
- leetcode_104题——Maximum Depth of Binary Tree (二叉树,递归,队列,还有递归没想出来)