//中序
class Solution {
/**
* @param root: The root of binary tree.
* @return: Inorder in vector which contains node values.
*/
public:
vector<int> a;
void look(TreeNode *root)
{
if(root==NULL)
return;
look(root->left);
a.push_back(root->val);
look(root->right);
}
vector<int> inorderTraversal(TreeNode *root)
{
look(root);
return a;
// write your code here
}
};
//层次遍历
#include<queue>
class Solution {
/**
* @param root: The root of binary tree.
* @return: Level order a list of lists of integer
*/
public:
vector<vector<int>> a;
vector<vector<int>> levelOrder(TreeNode *root)
{
queue<TreeNode *> q;
if(root==NULL)
return a;
q.push(root);
TreeNode *a1;
a1=new TreeNode;
vector<int> xx;
while(!q.empty())
{
xx.clear();
int x=q.size();
for(int j=0;j<x;j++)
{
a1=q.front();
q.pop();
int t=a1->val;
xx.push_back(t);
if(a1->left!=NULL)
q.push(a1->left);
if(a1->right!=NULL)
q.push(a1->right);
}
a.push_back(xx);
}
return a;
// write your code here
}
};
后续遍历
class Solution {
/**
* @param root: The root of binary tree.
* @return: Postorder in vector which contains node values.
*/
public:
vector<int> a;
void look(TreeNode *root)
{
if(root==NULL)
return ;
look(root->left);
look(root->right);
a.push_back(root->val);
}
vector<int> postorderTraversal(TreeNode *root) {
look(root);
return a;
// write your code here
}
};