[LeetCode] Maximum Depth of Binary Tree dfs,深度搜索
2015-01-08 18:16
567 查看
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.
Hide Tags
Tree Depth-first Search
简单的深度搜索
View Code
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
Hide Tags
Tree Depth-first Search
简单的深度搜索
#include <iostream> using namespace std; /** * 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 maxRet; int maxDepth(TreeNode *root) { maxRet=0; int curDep=1; help_f(root,curDep); return maxRet; } void help_f(TreeNode * root,int &curDep) { if(root==NULL) return ; curDep++; help_f(root->left,curDep); help_f(root->right,curDep); curDep--; if(root->left==NULL&&root->right==NULL){ if(maxRet<curDep) maxRet=curDep; } } }; int main() { return 0; }
View Code
相关文章推荐
- LeetCode 104 Maximum Depth of Binary Tree(二叉树的最大深度)
- [LeetCode-104] Maximum Depth of Binary Tree(二叉树最大深度)
- LeetCode | Maximum Depth of Binary Tree(二叉树的深度)
- leetcode:Maximum Depth of Binary Tree(计算二叉树深度) 【面试算法】
- Leetcode Maximum Depth of Binary Tree 二叉树最大深度
- [Java代码] [Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度
- 【LeetCode】 Maximum Depth of Binary Tree 二叉树的最大深度 - Easy
- 【LeetCode笔记】Maximum Depth of Binary Tree 二叉树最大深度 递归&非递归
- [leetcode]Maximum Depth of Binary Tree (求二叉树的最大深度 C语言)
- leetcode Maximum Depth of Binary Tree 树的深度
- LeetCode:104_Maximum Depth of Binary Tree | 二叉树的最大深度 | Easy
- LeetCode:104_Maximum Depth of Binary Tree | 二叉树的最大深度 | 深度优先与广度优先深入理解
- LeetCode 111 Minimum Depth of Binary Tree(二叉树的最短深度)(BT、DFS)(*)
- Leetcode No.104 Maximum Depth of Binary Tree 遍历二叉树的深度
- 【LeetCode】Maximum Depth of Binary Tree (二叉树最大深度)
- LeetCode Maximum Depth of Binary Tree (求树的深度)
- LeetCode--Maximum Depth of Binary Tree (检索二叉树的最大深度)Python
- LeetCode 104 Maximum Depth of Binary Tree(DFS)
- 【LeetCode】 Maximum Depth of Binary Tree 二叉树的最大深度
- leetcode 104 Maximum Depth of Binary Tree(DFS)