首页 > 代码库 > 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;}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。