简单粗暴实现:三个方法都遍历一下即可
/**
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
class Solution {
public:
/**
*
* @param root TreeNode类 the root of binary tree
* @return int整型vector<vector<>>
*/
void front(TreeNode* Node, vector<int> &f){
if(!Node) return;
f.push_back(Node->val);
front(Node->left, f);
front(Node->right, f);
}
void mid(TreeNode* Node, vector<int> &m){
if(!Node) return;
mid(Node->left, m);
m.push_back(Node->val);
mid(Node->right, m);
}
void back(TreeNode* Node, vector<int> &b){
if(!Node) return;
back(Node->left, b);
back(Node->right, b);
b.push_back(Node->val);
}
vector<vector<int> > threeOrders(TreeNode* root) {
// write code here
vector<int> f, m, b;
vector<vector<int>> all;
front(root, f);
mid(root, m);
back(root, b);
all.push_back(f);
all.push_back(m);
all.push_back(b);
return all;
}
};