104. Maximum Depth of Binary Tree(DFS)
来源:互联网 发布:域名解析端口号 编辑:程序博客网 时间:2024/06/06 13:08
1. Description
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.
2. Analysis
问题转为求该二叉树的高度,只要计算出该二叉树有几层即可,是一道求二叉树高度的基础题,问题关键是保存遍历过的节点,从而遍历该些节点的子节点。用vector或queue皆可。难度系数:easy
3. Algorithm achievement
算法的时间复杂度为
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: int maxDepth(TreeNode* root) { if(root == NULL) return 0; TreeNode * node = NULL; int level = 0; queue<TreeNode*> tmp; tmp.push(root); while( !tmp.empty()) { level ++; for(int i = 0, size = tmp.size(); i < size; i++) { node = tmp.front(); tmp.pop(); if(node->left != NULL) { tmp.push(node->left); } if(node->right != NULL) { tmp.push(node->right); } } } return level; }};
阅读全文
0 0
- DFS:104. Maximum Depth of Binary Tree
- 【LeetCode】104. Maximum Depth of Binary Tree(DFS|BFS)
- 104. Maximum Depth of Binary Tree(DFS)
- LeetCode oj 104. Maximum Depth of Binary Tree(DFS||BFS)
- dfs-104. Maximum Depth of Binary Tree
- [DFS] leetcode 104. Maximum Depth of Binary Tree
- LeetCode - Maximum Depth of Binary Tree(DFS)
- LeetCode 104Maximum Depth of Binary Tree(DFS?)
- LeetCode 104 Maximum Depth of Binary Tree(DFS)
- Leetcode - Tree - 104. Maximum Depth of Binary Tree(DFS求二叉树最深深度)
- [LeetCode]104.Maximum Depth of Binary Tree
- 104.Maximum Depth of Binary Tree
- 【LeetCode】104.Maximum Depth of Binary Tree
- [Leetcode] 104. Maximum Depth of Binary Tree
- LeetCode --- 104. Maximum Depth of Binary Tree
- LeetCode 104. Maximum Depth of Binary Tree
- 104.Maximum Depth of Binary Tree
- 104. Maximum Depth of Binary Tree
- 前置两千万柔光自拍厂笔试题
- 前端系列——jquery前端国际化解决方案“填坑日记”
- vector内存增长方式
- leecode-385. Mini Parser
- html常用的符号实体
- 104. Maximum Depth of Binary Tree(DFS)
- mac搭建svn
- JQuery常用方法(1)
- 通过ssh连接开发板——arm-linux下ssh的移植
- 冒泡排序
- invalid bitcode signature解决方案
- WS彩票游戏完整商业程序源码下载
- python用quad、dblquad实现一维二维积分范例
- Oracle用户安全及用户资源限制文件