热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

leetcode104.MaximumDepthofBinaryTree(C语言数据结构,二叉树的深度,递归)30

贴原题:Givenabinarytree,finditsmaximumdepth.Themaximumdepthisthenumberofnodesalongthel

贴原题:

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.

解析:
  本题是要求二叉树的深度。这应该算是数据结构最基础的题目了。

C代码:

/*** Definition for a binary tree node.* struct TreeNode {* int val;* struct TreeNode *left;* struct TreeNode *right;* };*/
int maxDepth(struct TreeNode* root) {int l_depth=0, r_depth=0;if(root!=NULL)//根节点非空则继续向下遍历{l_depth=maxDepth(root->left)+1;//深度+1r_depth=maxDepth(root->right)+1;}return l_depth>r_depth?l_depth:r_depth;//取左右最大值
}


推荐阅读
author-avatar
浪漫族屋nr
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有