劍指offer32(1,2,3)--從上往下列印二叉樹,分行從上往下列印二叉樹,按之字形順序列印二叉樹
阿新 • • 發佈:2018-12-31
從上往下列印二叉樹
題目描述
從上往下打印出二叉樹的每個節點,同層節點從左至右列印。
思路:
層序遍歷,用到佇列
/* 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> res; queue<TreeNode*> que; if(root==NULL) return res; que.push(root); while(!que.empty()) { TreeNode* tmp=que.front(); que.pop(); res.push_back(tmp->val); if(tmp->left!=NULL) que.push(tmp->left); if(tmp->right!=NULL) que.push(tmp->right); } return res; } };
把二叉樹列印成多行
題目描述:
從上到下按層列印二叉樹,同一層結點從左至右輸出。每一層輸出一行。
思路:
層序遍歷,然後用一個變數記錄下一層結點數,再用一個變數記錄已經儲存的這一層的點數。
/* struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { } }; */ class Solution { public: vector<vector<int> > Print(TreeNode* pRoot) { vector<vector<int> > res; queue<TreeNode*> que; if(!pRoot) return res; que.push(pRoot); int levelnum = 0; int number = 1; vector<int> tmpv; while (!que.empty()) { TreeNode* tmp = que.front(); que.pop(); tmpv.push_back(tmp->val); if (tmp->left != NULL){ que.push(tmp->left); levelnum++; } if (tmp->right != NULL){ que.push(tmp->right); levelnum++; } --number; if (number == 0) { res.push_back(tmpv); number = levelnum; levelnum = 0; tmpv.clear(); } } return res; } };
連結:https://www.nowcoder.com/questionTerminal/445c44d982d04483b04a54f298796288 來源:牛客網 層次遍歷 主要是每一次遍歷完一層,可以在大while中新增一個while迴圈,這個的條件就是佇列中剩餘的元素個數,這個元素個數在遍歷的過程中就是下一層元素的個數啦。 class Solution { public: vector<vector<int> > Print(TreeNode* pRoot) { vector<vector<int> > vec; if(pRoot == NULL) return vec; queue<TreeNode*> q; q.push(pRoot); while(!q.empty()) { int lo = 0, hi = q.size(); vector<int> c; while(lo++ < hi) { TreeNode *t = q.front(); q.pop(); c.push_back(t->val); if(t->left) q.push(t->left); if(t->right) q.push(t->right); } vec.push_back(c); } return vec; } };
思路:
用兩個佇列來儲存層序遍歷的點
連結:https://www.nowcoder.com/questionTerminal/445c44d982d04483b04a54f298796288
來源:牛客網
//兩個佇列交錯列印
class Solution {
public:
vector<vector<int> > Print(TreeNode* pRoot) {
vector<vector<int>> ret;
if(pRoot==NULL) return ret;
queue<TreeNode*> q1;
queue<TreeNode*> q2;
q1.push(pRoot);
while(!q1.empty()||!q2.empty()){
vector<int> v1;
vector<int> v2;
while(!q1.empty()){
v1.push_back(q1.front()->val);
if(q1.front()->left!=NULL) q2.push(q1.front()->left);
if(q1.front()->right!=NULL) q2.push(q1.front()->right);
q1.pop();
}
if(v1.size()!=0)
ret.push_back(v1);
while(!q2.empty()){
v2.push_back(q2.front()->val);
if(q2.front()->left!=NULL) q1.push(q2.front()->left);
if(q2.front()->right!=NULL) q1.push(q2.front()->right);
q2.pop();
}
if(v2.size()!=0)
ret.push_back(v2);
}
return ret;
}
};
按之字形順序列印二叉樹
題目描述
請實現一個函式按照之字形列印二叉樹,即第一行按照從左到右的順序列印,第二層按照從右至左的順序列印,第三行按照從左到右的順序列印,其他行以此類推。
思路:
用兩個棧來儲存層序遍歷的點
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};
*/
class Solution {
public:
vector<vector<int> > Print(TreeNode* pRoot) {
vector<vector<int> > res;
if(pRoot==NULL) return res;
stack<TreeNode*> sta1,sta2;
sta1.push(pRoot);
while(!sta1.empty()||!sta2.empty())//這裡必須是或的關係,因為跳出的條件是兩個sta都為空
{
vector<int> v1;
vector<int> v2;
while(!sta1.empty())
{
TreeNode* tmp=sta1.top();
sta1.pop();
v1.push_back(tmp->val);
if(tmp->left!=NULL)
sta2.push(tmp->left);
if(tmp->right!=NULL)
sta2.push(tmp->right);
}
if(v1.size())//這裡也要有,不然可能會輸出空的最後一行
res.push_back(v1);
while(!sta2.empty())
{
TreeNode* tmp=sta2.top();
sta2.pop();
v2.push_back(tmp->val);
if(tmp->right!=NULL)
sta1.push(tmp->right);
if(tmp->left!=NULL)
sta1.push(tmp->left);
}
if(v2.size())//這裡也要有判斷,不然可能會輸出空的最後一行
res.push_back(v2);
}
return res;
}
};