Minimum Depth of Binary Tree
of Tree Binary minimum depth
2023-09-14 08:57:33 时间
二叉树的最小深度
采用递归的方式求左右结点的高度,注意判断一个结点是否是叶子结点(左右子树都不存大)。
int minDepth(TreeNode *root) { return minDepth(root, false); } int minDepth(TreeNode *root, bool hasbrothers) { if (root == nullptr)return hasbrothers ? INT_MAX : 0; return 1 + min(minDepth(root->left, root->right != nullptr), minDepth(root->right, root->left != nullptr)); }
同理可判断最大深度,因为是求最大值,所以无需判断该结点是否是叶子结点(如果不是叶子结点,肯定不是最大深度)。
相关文章
- 04-树5 Root of AVL Tree
- Cannot read property 'bindings' of null
- HOG特征(Histogram of Gradient)学习总结
- 【阅读】A Comprehensive Survey on Distributed Training of Graph Neural Networks——翻译
- WordPress 遗嘱插件:Next of Kin
- ORA-23480: Column string is not a top-level column of “string”.”string”. ORACLE 报错 故障修复 远程处理
- ORA-14609: Tablespace was not specified for the previous lob segments of column string in template but is specified for string ORACLE 报错 故障修复 远程处理
- ORA-16072: a minimum of one standby database destination is required ORACLE 报错 故障修复 远程处理
- ORA-19020: invalid dereference of XMLType columns ORACLE 报错 故障修复 远程处理
- PostgreSQL HV001: fdw_out_of_memory 报错 故障修复 远程处理
- Android开发中遇到的问题(五)——Eclipse导入Android项目出现”Invalid project description overlaps the location of another project”错误的解决办法详解手机开发
- The Power of Redis: Running on Windows 32 Bit(rediswin32)
- 创建list ALV tree[RS_TREE_LIST_DISPLAY]详解编程语言
- Reap the Benefits of Using MySQL WITH Clause(mysqlwith)
- Linux:Exploring the Capabilities of 64Bit Computing(linux64位)
- Linux系统下使用Tree命令查看目录结构(tree命令linux)
- Exploring the Power of Triangular Functions in Linux: A Comprehensive Guide(linux三角函数)
- Exploring the Essential Column Attributes of MySQL: A Guide for Beginners(mysql列属性)
- Exploring the Power of Echo Command in Linux: Everything You Need to Know(echolinux)