Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).
For example:
Given binary tree[3,9,20,null,null,15,7] ,
Given binary tree
3 / \ 9 20 / \ 15 7
return its bottom-up level order traversal as:
[ [15,7], [9,20], [3] ]<Solution>
這題是 Binary Tree Level Order Traversal 的衍生題
這次的 level 不是由上到下,而是要改成由下到上
可以完全用同樣觀念,只要改一行 code
就是將 level 的值丟到 ans 時,不要使用 push_back
而是改成 ans.insert(ans.begin(), out),把高 level 的值放到低 level 的值前面
code 如下
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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: | |
vector<vector<int>> levelOrderBottom(TreeNode* root) { | |
vector<vector<int>> ans; | |
queue<TreeNode *> nodeQue; | |
if(root) { | |
nodeQue.push(root); | |
} | |
int size; | |
vector<int> out; | |
while(!nodeQue.empty()) { | |
out.clear(); | |
size = nodeQue.size(); | |
for(int i = 0; i < size; i++) { | |
TreeNode *tmpNode = nodeQue.front(); | |
nodeQue.pop(); | |
out.push_back(tmpNode->val); | |
if(tmpNode->left) { | |
nodeQue.push(tmpNode->left); | |
} | |
if(tmpNode->right) { | |
nodeQue.push(tmpNode->right); | |
} | |
} | |
//> put higher level into front | |
ans.insert(ans.begin(),out); | |
} | |
return ans; | |
} | |
}; |
而且速度還比使用 vector::insert() 會快很多
code如下
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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: | |
vector<vector<int>> levelOrderBottom(TreeNode* root) { | |
vector<vector<int>> ans; | |
queue<TreeNode *> nodeQue; | |
if(root) { | |
nodeQue.push(root); | |
} | |
int size; | |
vector<int> out; | |
while(!nodeQue.empty()) { | |
out.clear(); | |
size = nodeQue.size(); | |
for(int i = 0; i < size; i++) { | |
TreeNode *tmpNode = nodeQue.front(); | |
nodeQue.pop(); | |
out.push_back(tmpNode->val); | |
if(tmpNode->left) { | |
nodeQue.push(tmpNode->left); | |
} | |
if(tmpNode->right) { | |
nodeQue.push(tmpNode->right); | |
} | |
} | |
ans.push_back(out); | |
} | |
//> reverse the ans | |
reverse(ans.begin(), ans.end()); | |
return ans; | |
} | |
}; |
code 如下
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 { | |
private: | |
vector<vector<int>> ans; | |
public: | |
vector<vector<int>> levelOrderBottom(TreeNode* root) { | |
recursive(root, 0); | |
return ans; | |
} | |
void recursive(TreeNode *node, const int &level) { | |
if(!node) { | |
return; | |
} | |
//> current node is in new level | |
if(ans.size() == level) { | |
//> put higher level into front | |
ans.insert(ans.begin(), vector<int>()); | |
} | |
ans[ans.size() - 1 - level].push_back(node->val); | |
recursive(node->left, level+1); | |
recursive(node->right, level+1); | |
} | |
}; |
code 如下
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 { | |
private: | |
vector<vector<int>> ans; | |
public: | |
vector<vector<int>> levelOrderBottom(TreeNode* root) { | |
recursive(root, 0); | |
reverse(ans.begin(), ans.end()); | |
return ans; | |
} | |
void recursive(TreeNode *node, const int &level) { | |
if(!node) { | |
return; | |
} | |
//> current node is in new level | |
if(ans.size() == level) { | |
ans.push_back({}); | |
} | |
ans[level].push_back(node->val); | |
recursive(node->left, level+1); | |
recursive(node->right, level+1); | |
} | |
}; |
沒有留言:
張貼留言