非常easy的题目.只是还是认为要说一下. 最小深度.非常快想到bfs,层序遍历嘛.本科的时候实在是没写过多少代码,一開始竟然想不到怎么保存一层的信息.后来想到能够压入一个特殊的对象,每次到达这个对象就知道是一层了.我用的是空指针.认为这个适用性还是不错的.一层的节点入队结束后,应该压入一个NULL.当一层的节点都处理完.遇到NULL的时候,要在队列尾部再入队一个NULL,这是后一层的分界线嘛. 昨天在还有一道题上看到了还有一种做法.用一个数据结构vector<set<*> >(2…
这道题预计是ac率最高的一道了.你当然能够用层序遍历,我佩服你的耐心和勇气.由于看到别人的三行代码,会不会流眼泪呢.. class Solution { public: int maxDepth(TreeNode *root) { if(root == NULL) return 0; if(!root->left&&!root->right) return 1; return max(maxDepth(root->left), maxDepth(root->righ…
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:https://leetcode.com/problems/maximum-depth-of-binary-tree/ Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along th…
Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 和上一题对应,求二叉树的最小深度. 解题思路: 参考上一题Maximun Depth of Binary Tree中最后那…
Maximum Depth of Binary Tree 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. 求二叉树的高度,即从根节点到叶节点上所有节点的数量. 解题思路: 这题是难度系数1,频率1的题目……用到的知识点是二叉树D…
Minimum Depth of Binary Tree Total Accepted: 24760 Total Submissions: 83665My Submissions Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.…
Maximum Depth of Binary Tree  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. 与Minimum Depth of Binary Tree对照看 解法一:递归,子树高度+1. /** * Defini…
Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 与Maximum Depth of Binary Tree对照看 解法一:深度优先遍历 借助栈进行深度优先遍历(DFS),…
Problem Link: http://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ To find the minimum depth, we BFS from the root and record the depth. For each level we add 1 to the depth and return the depth value when we reach a leaf. The python code is…
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf nod…