从上到下打印出二叉树的每个节点,同一层的节点按照从左到右的顺序打印。
例如:
给定二叉树: [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
返回:[3,9,20,15,7]
======================================================
基本的广度优先遍历,借助队列
class Solution { public: vector<int> levelOrder(TreeNode* root) { vector<int> v; if (root == NULL) return v; queue<TreeNode *> q; q.push(root); while (!q.empty()) { TreeNode* r = q.front(); v.push_back(r->val); if (r->left) q.push(r->left); if (r->right) q.push(r->right); q.pop(); } return v; } };