[剑指offer] 22. 从上往下打印二叉树
题目描述
从上往下打印出二叉树的每个节点,同层节点从左至右打印。
思路:
思路:
队列
class Solution { public: vector<int> PrintFromTopToBottom(TreeNode *root) { vector<int> res; if (!root) return res; queue<TreeNode *> que; que.push(root); while (!que.empty()) { TreeNode *cur = que.front(); que.pop(); if(cur) res.push_back(cur->val); if(cur->left) que.push(cur->left); if(cur->right) que.push(cur->right); } return res; } };