題目:
打印的結果是 :A B C D E F
剛纔是我想的是用遞歸 但是遞歸發現不是這樣
需要藉助個容器 裝着一層的節點 一層一層的向下打印 直到這個容器爲空
/*
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) {
vector<int> m_vec;
queue<TreeNode*> t;
if(root == nullptr)
return m_vec;
t.push(root);
while (!t.empty())
{
m_vec.push_back(t.front()->val);
if (t.front()->left != nullptr)
{
t.push(t.front()->left);
}
if (t.front()->right != nullptr)
{
t.push(t.front()->right);
}
t.pop();
}
return m_vec;
}
};