Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).
For example:
Given binary tree {3,9,20,#,#,15,7}
,
3 / 9 20 / 15 7
return its level order traversal as:
[ [3], [9,20], [15,7] ]
思路:每一次一层中的所有结点插入到一个vector<TreeNode*>中,然后将这一层中的vector<TreeNode*>插入到包含所有层的vector<vector<TreeNode*> >中。从根结点开始,每次取出一层,访问该层的所以结点,如果其左右子树不为空,则将其左右子树加入到vector<TreeNode*>中,最后在将这一层插入到vector<vector<TreeNode*>>中,每次都是针对最后一层进行的操作。
C++代码实现:
#include<iostream> #include<new> #include<vector> using namespace std; //Definition for binary tree struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; class Solution { public: vector<vector<int> > levelOrder(TreeNode *root) { vector<vector<int> > vec; vector<vector<TreeNode*> > rvec; size_t i=0; if(root==NULL) return vector<vector<int> >(); vec.push_back({root->val}); rvec.push_back({root}); vector<int> v1; vector<TreeNode*> v2; while(vec.size()&&rvec.size()) { v1.clear(); v2.clear(); for(i=0; i<rvec[rvec.size()-1].size(); i++) { cout<<"level : "<<vec.size()<<"val: "<<vec[rvec.size()-1][i]<<endl; TreeNode *tmp=rvec[rvec.size()-1][i]; cout<<"level : "<<vec.size()<<"node: "<<tmp->val<<endl; if(tmp->left) { v1.push_back(tmp->left->val); v2.push_back(tmp->left); } if(tmp->right) { v1.push_back(tmp->right->val); v2.push_back(tmp->right); } } if(!v1.empty()&&!v2.empty()) { vec.push_back(v1); rvec.push_back(v2); } else break; } return vec; } void createTree(TreeNode *&root) { int i; cin>>i; if(i!=0) { root=new TreeNode(i); if(root==NULL) return; createTree(root->left); createTree(root->right); } } }; int main() { Solution s; TreeNode *root; s.createTree(root); vector<vector<int> > vec=s.levelOrder(root); for(auto a:vec) { for(auto v:a) cout<<v<<" "; cout<<endl; } }
运行结果: