标签:cpp pop node 遍历 subject ref solution lin continue
/* struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { } };*/ class Solution { public: vector<int> PrintFromTopToBottom(TreeNode* root) { } };
借助队列进行bfs遍历
AC代码:
class Solution { public: vector<int> PrintFromTopToBottom(TreeNode* root) { queue<TreeNode*> que; vector<int> vec; if(root == NULL) return vec; que.push(root); while(!que.empty()) { TreeNode* tr; tr = que.front(); que.pop(); if(tr == NULL) continue; vec.push_back(tr->val); que.push(tr->left); que.push(tr->right); } return vec; } };
标签:cpp pop node 遍历 subject ref solution lin continue
原文地址:https://www.cnblogs.com/Hyouka/p/9250552.html