
解法一:递归方式
/**
* 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 preOrder(vector<int> &ans,TreeNode* root){
if(root==NULL)
return ;
ans.push_back(root->val);
preOrder(ans, root->left);
preOrder(ans, root->right);
}
void inOrder(vector<int> &ans,TreeNode* root){
if(root==NULL)
return ;
inOrder(ans, root->left);
ans.push_back(root->val);
inOrder(ans, root->right);
}
void postOrder(vector<int> &ans,TreeNode* root){
if(root==NULL)
return ;
postOrder(ans, root->left);
postOrder(ans, root->right);
ans.push_back(root->val);
}
vector<vector<int> > threeOrders(TreeNode* root) {
// write code here
vector<vector<int> > ans(3);
preOrder(ans[0],root);
inOrder(ans[1],root);
postOrder(ans[2],root);
return ans;
}
};
解法二:非递归方式
/**
* 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 preOrder(vector<int> &ans,TreeNode* root)
{
stack<TreeNode*>st;
st.push(root);
while(!st.empty())
{
TreeNode* now=st.top();
st.pop();
if(now==NULL)
continue;
ans.push_back(now->val);
st.push(now->right);
st.push(now->left);
}
}
void inOrder(vector<int> &ans,TreeNode* root)
{
stack<pair<TreeNode*,int> >st;
st.push(pair<TreeNode*,int>(root,0));
while(!st.empty()){
TreeNode* now=st.top().first;
int vis=st.top().second;
if(now==NULL){
st.pop();
continue;
}
if(vis==1||now->left==NULL){
ans.push_back(now->val);
st.pop();
if(now->right!=NULL)
st.push(pair<TreeNode*,int>(now->right,0));
}
else{
st.top().second=1;
st.push(pair<TreeNode*,int>(now->left,0));
}
}
}
void postOrder(vector<int> &ans,TreeNode* root)
{
stack<pair<TreeNode*,int> >st;
st.push(pair<TreeNode*,int>(root,0));
while(!st.empty()){
TreeNode* now=st.top().first;
int vis=st.top().second;
if(now==NULL){
st.pop();
continue;
}
if(vis==1){
ans.push_back(now->val);
st.pop();
}
else{
st.top().second=1;
st.push(pair<TreeNode*,int>(now->right,0));
st.push(pair<TreeNode*,int>(now->left,0));
}
}
}
vector<vector<int> > threeOrders(TreeNode* root) {
// write code here
vector<vector<int> > ans(3);
preOrder(ans[0],root);
inOrder(ans[1],root);
postOrder(ans[2],root);
return ans;
}
};

/**
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
class Solution {
public:
/**
*
* @param root TreeNode类
* @return int整型vector<vector<>>
*/
vector<vector<int> > levelOrder(TreeNode* root) {
// write code here
vector<vector<int> >ans;
queue<pair<TreeNode*,int> >q;
q.push(pair<TreeNode*,int>(root,0));
while(!q.empty())
{
TreeNode* now=q.front().first;
int ct=q.front().second;
q.pop();
if(now==NULL)
continue;
vector<int>tmp;
if(ct+1>ans.size())
ans.push_back(tmp);
ans[ct].push_back(now->val);
if(now->left!=NULL)
q.push(pair<TreeNode*,int>(now->left,ct+1));
if(now->right!=NULL)
q.push(pair<TreeNode*,int>(now->right,ct+1));
}
return ans;
}
};

/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};
*/
class Solution {
public:
vector<vector<int> > Print(TreeNode* pRoot) {
vector<vector<int> >ans;
queue<pair<TreeNode*,int> >q;
q.push(pair<TreeNode*,int>(pRoot,0));
while(!q.empty())
{
TreeNode* now=q.front().first;
int ct=q.front().second;
q.pop();
if(now==NULL)
continue;
vector<int>tmp;
if(ct+1>ans.size())
ans.push_back(tmp);
ans[ct].push_back(now->val);
if(now->left!=NULL)
q.push(pair<TreeNode*,int>(now->left,ct+1));
if(now->right!=NULL)
q.push(pair<TreeNode*,int>(now->right,ct+1));
}
for(int i=1;i<ans.size();i+=2)
reverse(ans[i].begin(),ans[i].end());
return ans;
}
};

/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
void Build(TreeNode* &root,vector<int> &pre, vector<int> &vin,int ps,int is,int len){
if(len==0){
root=NULL;
return;
}
int Root=pre[ps];
root=new TreeNode(Root);
int lnum=0;
int rnum=0;
for(int i=0;i<len;i++){
if(vin[is+i]!=Root)
lnum++;
else
break;
}
rnum=len-lnum-1;
Build(root->left,pre,vin,ps+1,is,lnum);
Build(root->right,pre,vin,ps+lnum+1,is+lnum+1,rnum);
}
TreeNode* reConstructBinaryTree(vector<int> pre, vector<int> vin)
{
// write code here
TreeNode* root=NULL;
Build(root,pre,vin,0,0,pre.size());
return root;
}
};

/**
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
class Solution {
public:
/**
*
* @param root TreeNode类
* @return int整型
*/
int ans=0;
void dfs(TreeNode* root,int num)
{
if(root==NULL)
return;
int now=num*10+root->val;
if(root->left==NULL&&root->right==NULL)
ans+=now;
if(root->left!=NULL)
dfs(root->left,now);
if(root->right!=NULL)
dfs(root->right,now);
}
int sumNumbers(TreeNode* root)
{
dfs(root,0);
return ans;
}
};

class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
* 求二叉树的右视图
* @param xianxu int整型vector 先序遍历
* @param zhongxu int整型vector 中序遍历
* @return int整型vector
*/
TreeNode* CreateTree(vector<int> &x, vector<int> &z,int lx,int rx,int lz,int rz)
{
if(lx > rx)
return NULL;
TreeNode* root = new TreeNode(x[lx]);
int mid = lz;
while(mid<=rz)
{
if(x[lx] == z[mid])
break;
mid++;
}
root->left=CreateTree(x, z, lx+1, rx-(rz-mid), lz, mid-1);
root->right=CreateTree(x, z, rx-(rz-mid)+1, rx, mid+1, rz);
return root;
}
vector<int> solve(vector<int>& xianxu, vector<int>& zhongxu) {
// write code here
vector<int> res;
if(xianxu.empty()||zhongxu.empty()) return res;
TreeNode* root = CreateTree(xianxu, zhongxu, 0, xianxu.size()-1, 0, zhongxu.size()-1);
queue<TreeNode*> q;
q.push(root);
while(!q.empty())
{
int len = q.size();
while(--len)
{
TreeNode* tmp = q.front();
if(tmp->left)
q.push(tmp->left);
if(tmp->right)
q.push(tmp->right);
q.pop();
}
TreeNode* tmp=q.front();
if(tmp->left)
q.push(tmp->left);
if(tmp->right)
q.push(tmp->right);
res.push_back(tmp->val);
q.pop();
}
return res;
}
};