题解 | #合并两个有序的数组#
实现二叉树先序,中序和后序遍历
http://www.nowcoder.com/practice/a9fec6c46a684ad5a3abd4e365a9d362
class Solution {
public:
/**
*
* @param root TreeNode类 the root of binary tree
* @return int整型vector<vector<>>
*/
vector<int> v1,v2,v3;
vector<int> first(TreeNode* root){
if(!root) return v1;
v1.push_back(root->val);
first(root->left);
first(root->right);
return v1;
}
vector<int> mid(TreeNode* root){
if(!root) return v2;
mid(root->left);
v2.push_back(root->val);
mid(root->right);
return v2;
}
vector<int> last(TreeNode* root){
if(!root) return v3;
last(root->left);
last(root->right);
v3.push_back(root->val);
return v3;
}
vector<vector<int> > threeOrders(TreeNode* root) {
// write code here
vector<vector<int>> v4;
v4.push_back(first(root));
v4.push_back(mid(root));
v4.push_back(last(root));
return v4;
}
};
public:
/**
*
* @param root TreeNode类 the root of binary tree
* @return int整型vector<vector<>>
*/
vector<int> v1,v2,v3;
vector<int> first(TreeNode* root){
if(!root) return v1;
v1.push_back(root->val);
first(root->left);
first(root->right);
return v1;
}
vector<int> mid(TreeNode* root){
if(!root) return v2;
mid(root->left);
v2.push_back(root->val);
mid(root->right);
return v2;
}
vector<int> last(TreeNode* root){
if(!root) return v3;
last(root->left);
last(root->right);
v3.push_back(root->val);
return v3;
}
vector<vector<int> > threeOrders(TreeNode* root) {
// write code here
vector<vector<int>> v4;
v4.push_back(first(root));
v4.push_back(mid(root));
v4.push_back(last(root));
return v4;
}
};