void levelOrderBottom(TreeNode *root, int depth, vector<vector<int> >& ans) { if(!root) return; while(ans.size() < depth+1) ans.push_back(vector<int>()); ans[depth].push_back(root->val); levelOrderBottom(root->left, depth+1, ans); levelOrderBottom(root->right, depth+1, ans); }
vector<vector<int> > levelOrderBottom(TreeNode *root) { vector<vector<int>> myVector; if (root == NULL) return myVector; else{ vector<vector<int>> leftVector = levelOrderBottom(root->left); vector<vector<int>> rightVector = levelOrderBottom(root->right); int leftSize = leftVector.size(); int rightSize = rightVector.size(); vector<int> inner; inner.push_back(root->val); if(leftSize >= rightSize){ while(rightSize){ rightSize--; leftSize--; leftVector[leftSize].insert(leftVector[leftSize].end(), rightVector[rightSize].begin(), rightVector[rightSize].end()); } myVector.insert(myVector.begin(), leftVector.begin(), leftVector.end()); myVector.push_back(inner); } else{ while(leftSize){ leftSize--; rightSize--; rightVector[rightSize].insert(rightVector[rightSize].begin(), leftVector[leftSize].begin(), leftVector[leftSize].end()); } myVector.insert(myVector.begin(), rightVector.begin(), rightVector.end()); myVector.push_back(inner); } } return myVector; }
int main() { struct TreeNode root, left, right; root.val = 1; root.left = &left; root.right= &right; left.val = 5; left.left = NULL; left.right = NULL; right.val = 4; right.left = NULL; right.right = NULL; int* columnSizes; int returnSize; int **tmp = levelOrderBottom(&root, &columnSizes, &returnSize); int i, j; for(i = 0; i < returnSize; i++) { for(j = 0; j < *(columnSizes + i); j ++) { printf("%d ", tmp[i][j]); } printf("\n"); } }
void test() { TreeNode *root = Help::decodeTree("3,9,20,#,#,15,7"); vector<vector<int>> result = levelOrderBottom(root); for (auto v : result) { for (auto i : v) { cout << ' ' << i; } cout << endl; } }
// ----------------------------------------------------------------------------- //virtual bool Solution::Test() { bool pass = true; { int nodes[] = { }; BTNode<int> * root = bt_create(nodes, sizeof(nodes) / sizeof(int)); std::vector< std::vector<int> > result = levelOrderBottom(root); pass = pass && result.empty(); bt_delete(root); } { int nodes[] = { 1 }; BTNode<int> * root = bt_create(nodes, sizeof(nodes) / sizeof(int)); std::vector< std::vector<int> > result = levelOrderBottom(root); pass = pass && (result.size() == 1) && (result[0].size() == 1) && (result[0][0] == 1) ; bt_delete(root); } { int nodes[] = { 3, 9, 20, '#', '#', 15, 7 }; BTNode<int> * root = bt_create(nodes, sizeof(nodes) / sizeof(int)); std::vector< std::vector<int> > v = levelOrderBottom(root); pass = pass && (v.size() == 3) && (v[0].size() == 2) && (v[0][0] == 15) && (v[0][1] == 7) && (v[1].size() == 2) && (v[1][0] == 9) && (v[1][1] == 20) && (v[2].size() == 1) && (v[2][0] == 3) ; bt_delete(root); } return pass; }
vector<vector<int> > levelOrderBottom(TreeNode *root) { vector<vector<int> > ans; levelOrderBottom(root, 0, ans); reverse(ans.begin(), ans.end()); return ans; }
void testLevelOrderBottom(){ TreeNode *root = new TreeNode(1); levelOrderBottom(root); cout<<"a"<<endl; }