标签:front temp 二叉树 str size turn push init tree node
1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution 11 { 12 vector<int> ans; 13 public: 14 int countNodes(TreeNode* root) 15 { 16 if(!root) return 0; 17 queue<TreeNode*> q; 18 q.push(root); 19 while(!q.empty()) 20 { 21 int n = q.size(); 22 for(int i = 0;i < n;i ++) 23 { 24 TreeNode* temp = q.front(); 25 q.pop(); 26 ans.push_back(temp->val); 27 28 if(temp->left) q.push(temp->left); 29 if(temp->right) q.push(temp->right); 30 } 31 } 32 return ans.size(); 33 } 34 };
标签:front temp 二叉树 str size turn push init tree node
原文地址:https://www.cnblogs.com/yuhong1103/p/12681167.html