1 class Solution { 2 public: 3 vector<vector<int> > Print(TreeNode* pRoot) { 4 vector<vector<int>> res; 5 if(pRoot == NULL) 6 return res; 7 stack<TreeNode*> num[2]; 8 int flag[2]; 9 int i=0; 10 num[0].push(pRoot); 11 // flag[0]=1; 12 while(!num[0].empty() || !num[1].empty()) 13 { 14 vector<int> prin; 15 while(!num[i].empty()) 16 { 17 18 prin.push_back(num[i].top()->val); 19 if(i==1) 20 { 21 if(num[i].top()->right) 22 num[1-i].push(num[i].top()->right); 23 if(num[i].top()->left) 24 num[1-i].push(num[i].top()->left); 25 } 26 else 27 { 28 if(num[i].top()->left) 29 num[1-i].push(num[i].top()->left); 30 if(num[i].top()->right) 31 num[1-i].push(num[i].top()->right); 32 } 33 num[i].pop(); 34 35 } 36 res.push_back(prin); 37 i=1-i; 38 } 39 return res; 40 } 41 42 };
时间: 2024-10-29 19:08:45