二叉树深度
来源:互联网 发布:上海德颐网络正规吗 编辑:程序博客网 时间:2024/05/21 15:40
递归
/*struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { }};*/class Solution {public: int TreeDepth(TreeNode* pRoot) { if(pRoot==nullptr) return 0; int left_depth=TreeDepth(pRoot->left); int right_depth=TreeDepth(pRoot->right); return left_depth>right_depth ? (left_depth+1):(right_depth+1); }};
层次遍历,需要用queue而不用stack
class Solution {public: int TreeDepth(TreeNode* pRoot) { queue<TreeNode*> q; if(!pRoot) return 0; q.push(pRoot); int level=0; while(!q.empty()){ int len=q.size(); level++; while(len--){ TreeNode* tem=q.front(); q.pop(); if(tem->left) q.push(tem->left); if(tem->right) q.push(tem->right); } } return level; }};
1 0
- 二叉树的深度
- 二叉树深度求法
- 二叉树深度算法
- 二叉树深度求法
- 二叉树的深度
- 二叉树的深度
- 二叉树的深度
- 二叉树深度
- 二叉树深度遍历
- 二叉树的深度
- 二叉树的深度
- 计算二叉树深度
- 深度遍历二叉树
- 求二叉树深度
- 二叉树的深度
- 求二叉树深度
- 二叉树的深度
- 二叉树的深度
- servlet3.0新的变化
- 乐观锁 悲观锁
- Shiro 设置session超时时间
- 刷题总结#8
- A-Frame WebVR开发入门教程
- 二叉树深度
- java对象序列化与反序列化
- css基础一(权重)
- Redis 学习笔记3:Jedis 连接虚拟机下的Redis 服务
- QiYuAdmin-部门增删改查
- 石头匠人的个人博客
- 蓝桥 还款计算
- Poj1847(floyd)
- spring MVC 传入 json 数组