标签:
https://leetcode.com/problems/count-complete-tree-nodes/
宽度优先搜索方法,超时!!
/** * 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: int countNodes(TreeNode* root) { int count=0; queue<TreeNode *> q1; if(root==NULL) return 0; q1.push(root); while(!q1.empty()) { TreeNode * temp=q1.front(); q1.pop(); count++; if(temp->left!=NULL) q1.push(temp->left); if(temp->right!=NULL) q1.push(temp->right); } return count; } };
标签:
原文地址:http://www.cnblogs.com/aguai1992/p/4780452.html