您的位置:首页 > 其它

LeetCode之maxDepth非递归解法

2014-03-28 20:56 148 查看
int  maxDepth_2(TreeNode* root)  //非递归求二叉树最大深度
{
TreeNode *p,*have_visited;
int treeDepth=0;
vector<TreeNode*> v_tree;
p=root;
have_visited=NULL;
while(p||!v_tree.empty())
{
if (p)
{
v_tree.push_back(p);
p=p->left;
}
else
{
p=v_tree.back();
if (p->right==NULL||p->right==have_visited)
{
treeDepth=treeDepth<v_tree.size()?v_tree.size():treeDepth;
have_visited=p;
v_tree.pop_back();
p=NULL;
}
else p=p->right;
}
}
return treeDepth;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐