剑指offer 面试题32 - I. 从上到下打印二叉树 [中等]——queue

我的解题:

层次遍历

class Solution {
public:
    vector<int> levelOrder(TreeNode* root) {
        vector<int> res;
        if(root==NULL)  return res;
        queue<TreeNode*> q;
        q.push(root);
        TreeNode* tmp;
        while(!q.empty()){
            tmp=q.front();
            q.pop();
            res.push_back(tmp->val);
            if(tmp->left)   q.push(tmp->left);
            if(tmp->right)  q.push(tmp->right);
        }
        return res;
    }
};

发布了76 篇原创文章 · 获赞 1 · 访问量 586

猜你喜欢

转载自blog.csdn.net/qq_41041762/article/details/105582352
今日推荐