首页 > 代码库 > interview fb2

interview fb2

2014.7.8fb

#include <iostream>using namespace std;struct TreeNode{    int val;    TreeNode *left;    TreeNode *right;    TreeNode(int val)    {        left = NULL;        right = NULL;    }};/*Input : binary tree      A     /     B   C   / \     D   E   G       \           F   HOutput : ABCDEGFH*/vector<vector<int> > transform(TreeNode *root){    vector<vector<int> ans;    if(root == NULL)        return ans;        vector<int> ans_piece;    queue<TreeNode *> last_time;        queue.push_back(root);    TreeNode *temp;        vector<int> num_each;    num_each.push_back(1);    num_each.push_back(0);    int level = 0;    while(queue.empty() == false)    {        num_each[level]--;                temp = queue.get_first_one();        queue.pop();        if(temp)            ans_piece.push_back(temp->val);        if(temp->left)        {            queue.push_back(temp->left);            num_each[level+1]++;        }        if(temp->right)        {            queue.push_back(temp->right);            num_each[level+1]++;        }                    if(num_each[level] == 0)        {            ans.push_back(ans_piece);            level++;            ans_piece.clear();            num_each.push_back(0);        }    }    return ans;}