逐层打印二叉树
作者:互联网
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; }
标签:pright,temp,打印,nullptr,逐层,BinaryTreeNode,二叉树,ans,push 来源: https://www.cnblogs.com/buctyk/p/13336791.html