请问有没有人会解答,帮助我一下,非常

img

请问有没有人会解答,帮助我一下,我不会,帮我答一下吧,如果有会的


class Solution {
public:
    vector<vector<int> > Print(TreeNode* pRoot) {
        vector<vector<int>> ans;
        if(!pRoot) return ans;
        stack<TreeNode*> stack1;
        stack<TreeNode*> stack2;
        stack1.push(pRoot);
        while(!stack1.empty() || !stack2.empty()){
            vector<int> tmp_ans1;
            while(!stack1.empty()){//出栈顺序是从左到右时,将子节点也按从左到右的顺序压入stack2,这样stack2的出栈顺序就是从右往左
                TreeNode* tmp = stack1.top();
                stack1.pop();
                tmp_ans1.push_back(tmp->val);
                if(tmp->left){
                    stack2.push(tmp->left);
                }
                if(tmp->right){
                    stack2.push(tmp->right);
                }
            }
            if(!tmp_ans1.empty()) ans.push_back(tmp_ans1);
            tmp_ans1.clear();
            while(!stack2.empty()){//出栈顺序是从右到左时,将子节点也按照从右到左的顺序压住stack1,这样stack1的出栈顺序就是从左往右
                TreeNode* tmp = stack2.top();
                stack2.pop();
                tmp_ans1.push_back(tmp->val);
                if(tmp->right) stack1.push(tmp->right);
                if(tmp->left) stack1.push(tmp->left);
            }
            if(!tmp_ans1.empty()) ans.push_back(tmp_ans1);
        }
        return ans;
    }
    
};