#include <iostream>
#include <queue>
struct Node
{
int val;
Node * left;
Node * right;
Node(int v) :val(v), left(nullptr), right(nullptr) {};
};
int countMaxDepth(Node * node)
{
if (node == nullptr)
return 0;
std::queue<Node *> node_Queue;
Node * nowNode = node;
node_Queue.emplace(nowNode);
int maxDepth = 0;
Node * NextEnd = nullptr;
Node * curEnd = node;
while (1)
{
if (node_Queue.empty())
{
break;
}
nowNode = node_Queue.front();
node_Queue.pop();
if (nowNode->left != nullptr)
{
node_Queue.emplace(nowNode->left);
NextEnd = nowNode->left;
}
if (nowNode->right != nullptr)
{
node_Queue.emplace(nowNode->right);
NextEnd = nowNode->right;
}
if (nowNode == curEnd)
{
curEnd = NextEnd;
maxDepth++;
}
}
return maxDepth;
}
int main()
{
Node * begin = new Node(1);
Node * nowpos = begin;
nowpos->left = new Node(2);
nowpos->left->left = new Node(3);
nowpos->left->right = new Node(3);
begin->right = new Node(4);
begin->right->left = new Node(5);
std:: cout << countMaxDepth(begin);
return 0;
}
© 允许规范转载