题目描述
从上往下打印出二叉树的每个节点,同层节点从左至右打印。
1 /* 2 struct TreeNode { 3 int val; 4 struct TreeNode *left; 5 struct TreeNode *right; 6 TreeNode(int x) : 7 val(x), left(NULL), right(NULL) { 8 } 9 };*/ 10 class Solution { 11 public: 12 vector<int> PrintFromTopToBottom(TreeNode *root) { 13 vector<int> vv; 14 if (root == NULL) 15 return vv; 16 std::queue<TreeNode *> qq; 17 qq.push(root); 18 TreeNode *tem; 19 while(!qq.empty()) 20 { 21 tem = qq.front(); 22 qq.pop(); 23 vv.push_back(tem->val); 24 if (tem ->left != NULL) 25 qq.push(tem->left); 26 if(tem ->right != NULL) 27 qq.push(tem->right); 28 } 29 return vv; 30 } 31 };