首页 > 其他 > 详细

逐层打印二叉树

时间:2020-07-18 21:53:54      阅读:58      评论:0      收藏:0      [点我收藏+]
struct BinaryTreeNode {
    int nvalue=0;
    BinaryTreeNode* pleft = nullptr;
    BinaryTreeNode* pright = nullptr;
    BinaryTreeNode* parent = nullptr;
};
vector<vector<int>> BinaryTreePrint(BinaryTreeNode* node) { vector<vector<int>> ans; if (node == nullptr) { throw exception("Invalid Input"); return ans; } queue<BinaryTreeNode*>q; q.push(node); while (!q.empty()) { int low = 0, high = q.size(); vector<int>v; while (low++ < high) { BinaryTreeNode* temp = q.front(); v.push_back(temp->nvalue); q.pop(); if (temp->pleft) { q.push(temp->pleft); } if (temp->pright) { q.push(temp->pright); } } ans.push_back(v); } return ans; }

 

逐层打印二叉树

原文:https://www.cnblogs.com/buctyk/p/13336791.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!