【剑指offer】二叉树的深度_solution1

题目:输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。

 

 1 /*
 2 struct TreeNode {
 3     int val;
 4     struct TreeNode *left;
 5     struct TreeNode *right;
 6     TreeNode(int x) :
 7             val(x), left(NULL), right(NULL) {
 8     }
 9 };*/
10 class Solution {
11 public:
12     int Max = -1;
13     int TreeDepth(TreeNode* pRoot)
14     {
15         preOrder(pRoot,0);
16         return Max;
17     }
18     void preOrder(TreeNode *r,int depth) {
19         if (r == NULL) {
20             if (depth > Max)
21                 Max = depth;
22             return;
23         }
24         preOrder(r->left, depth + 1);
25         preOrder(r->right, depth + 1);
26     }
27 };

 

posted @ 2018-11-27 20:56  Little_Shel  阅读(117)  评论(0编辑  收藏  举报