首页 > 其他 > 详细

剑指offer 二叉树深度

时间:2020-06-09 21:48:41      阅读:41      评论:0      收藏:0      [点我收藏+]

题目:

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

代码:

 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 TreeDepth(TreeNode* pRoot) {
13         if(pRoot == NULL) return 0;
14         return max(1+TreeDepth(pRoot->right),1+TreeDepth(pRoot->left));
15     }
16 };

我的笔记:

  二叉树深度递归遍历常规算法。

剑指offer 二叉树深度

原文:https://www.cnblogs.com/john1015/p/13080690.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!