/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> levelOrder(TreeNode* root) { // 层次遍历,默写题
vector<int> V;
queue<TreeNode*> Q;
TreeNode* p;
if(root) Q.push(root);
while(!Q.empty()) {
p = Q.front();
Q.pop();
V.push_back(p->val);
if(p->left) Q.push(p->left);
if(p->right) Q.push(p->right);
}
return V;
}
};
【leetcode 二叉树 C++】【剑指 Offer】 32 - I. 从上到下打印二叉树
最新推荐文章于 2023-08-02 19:15:28 发布