题解 | #JZ77 按之字形顺序打印二叉树#

用先进先出的队列来处理每一行的数字。用一个临时变量来接受该行的所有数据,当处理完之后,就需要把这个临时变量统计进结果中。

用一个times变量来记录当前层的结果是否需要反转。

注意:有传入pRoot为空的可能性。

/*
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>> ans;
        vector<TreeNode*> que;
        vector<int> tmp;
        ans.clear();
        que.clear();
        tmp.clear();
        
        if (!pRoot) return ans;
        
        
        que.push_back(pRoot);
        tmp.push_back(pRoot->val);
        ans.push_back(tmp);
        tmp.clear();
        
        int times = 1;
        while(que.size() != 0) {
//             tmp.clear();
            if (times&1) {
                int sz = que.size() - 1;
                for (int i = 0; i <= sz; i++) {
                    TreeNode* temp = que[0];
                    que.erase(que.begin());
                    if (temp->left) {
                        que.push_back(temp->left);
                        tmp.push_back(temp->left->val);
                    }
                        
                    if (temp->right) {
                        que.push_back(temp->right);
                        tmp.push_back(temp->right->val);
                    }
                }
                
                if (tmp.size() != 0) {
                    reverse(tmp.begin(), tmp.end());
                    ans.push_back(tmp);
                    tmp.clear();
                }
                    
            } else {
                int sz = que.size() - 1;
                for (int i = 0; i <= sz; i++) {
                    TreeNode* temp = que[0];
                    que.erase(que.begin());
                    if (temp->left) {
                        que.push_back(temp->left);
                        tmp.push_back(temp->left->val);
                    }
                        
                    if (temp->right) {
                        que.push_back(temp->right);
                        tmp.push_back(temp->right->val);
                    }
                }
                
                if (tmp.size() != 0) {
                    ans.push_back(tmp);
                    tmp.clear();
                }
            }
            times++;
        }
        return ans;
    }
    
};
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param pRoot TreeNode类 
# @return int整型二维数组
#
class Solution:
    def Print(self , pRoot: TreeNode) -> List[List[int]]:
        # write code here
        ans = []
        tmp = []
        que = []
#         times = 1
        if pRoot is None:
            return ans    
    
        que.append(pRoot)
        tmp.append(pRoot.val)
        ans.append(tmp)
#         tmp.clear()
        tmp = []
    
        
        
        times = 1
        while que.__len__() != 0:
            if times % 2 == 1:
                sz = que.__len__()
                for i in range(sz):
                    temp = que.pop(0)
                    
                    if temp.left:
                        que.append(temp.left)
                        tmp.append(temp.left.val)
                    
                    if temp.right:
                        que.append(temp.right)
                        tmp.append(temp.right.val)
                
                if tmp.__len__() != 0:
                    ans.append(tmp[::-1])
                    tmp = []
            else:
                sz = que.__len__()
                for i in range(sz):
                    temp = que.pop(0)
                    
                    if temp.left:
                        que.append(temp.left)
                        tmp.append(temp.left.val)
                    
                    if temp.right:
                        que.append(temp.right)
                        tmp.append(temp.right.val)
        
                if tmp.__len__() != 0:
                    ans.append(tmp)
                    tmp = []
                    
            times += 1
        return ans
全部评论

相关推荐

10-30 19:23
已编辑
山东大学(威海) C++
牛至超人:其实简历是不需要事无巨细的写的,让对方知道你有这段经历就行了,最重要的是面试的时候讲细讲明白
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务