/**
* 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 preorderTraversal(TreeNode* root) {
vector ret;
if(root==NULL)
return ret;
TreeNode* p;
stack s;
s.push(root);
while(!s.empty()){
p=s.top();
s.pop();
ret.push_back(p->val);
if(p->right!=NULL)
s.push(p->right);
if(p->left!=NULL)
s.push(p->left);
}
return ret;
}
};
class Solution {
public:
vector preorderTraversal(TreeNode* root) {
vector ret;
if(root==NULL)
return ret;
TreeNode* p=root;
stack s;
while(p!=NULL || !s.empty()){
while(p!=NULL){
s.push(p);
ret.push_back(p->val);
p=p->left;
}
if(!s.empty()){
p=s.top();
s.pop();
p=p->right;
}
}
return ret;
}
};
class Solution {
public:
vector inorderTraversal(TreeNode* root) {
vector ret;
if(root==NULL)
return ret;
stack s;
TreeNode* p=root;
while(p!=NULL || !s.empty()){
while(p!=NULL){
s.push(p);
p=p->left;
}
if(!s.empty()){
p=s.top();
s.pop();
ret.push_back(p->val);
p=p->right;
}
}
return ret;
}
};
class Solution {
public:
vector postorderTraversal(TreeNode* root) {
vector ret;
if(root==NULL)
return ret;
stack s;
s.push(root);
TreeNode* pre=NULL;//前一个输出结点
TreeNode* curr=root;
while(!s.empty()){
curr=s.top();
// 叶子结点 或者 它的子结点已经输出了(它有右结点因为先压右,那么curr->right==pre,否则curr->left==pre)
if( (curr->left==NULL && curr->right==NULL) || (pre != NULL) && (curr->left==pre || curr->right==pre) ){
ret.push_back(curr->val);
pre=curr;
s.pop();
}
else{
if(curr->right!=NULL)
s.push(curr->right);
if(curr->left!=NULL)
s.push(curr->left);
}
}
}
};
class Solution {
public:
TreeNode* buildTree(vector& inorder, vector& postorder) {
if(inorder.size()==0)
return NULL;
return dfs(inorder.begin(),inorder.end(),postorder.begin(),postorder.end());
}
TreeNode* dfs(vector::iterator inStart,vector::iterator inEnd,vector::iterator pStart,vector::iterator pEnd){
if(inStart==inEnd)
return NULL;
TreeNode* root=new TreeNode(*(pEnd-1));
vector::iterator it=find(inStart,inEnd,root->val);
int leftSize=it-inStart;
root->left=dfs(inStart,it,pStart,pStart+leftSize);
root->right=dfs(it+1,inEnd,pStart+leftSize,pEnd-1);
return root;
}
};
*/
class Solution {
public:
TreeNode* buildTree(vector& preorder, vector& inorder) {
if(preorder.size()==0)
return NULL ;
return dfs(preorder.begin(),preorder.end(),inorder.begin(),inorder.end());
}
//不包括preEnd,inEnd
TreeNode* dfs(vector::iterator preStart,vector::iterator preEnd,vector::iterator inStart,vector::iterator inEnd) {
if(preStart==preEnd)
return NULL;
TreeNode* root=new TreeNode(*preStart);
vector::iterator it=find(inStart,inEnd,root->val);
int leftSize=it-inStart;
root->left=dfs(preStart+1,preStart+1+leftSize,inStart,it);
root->right=dfs(preStart+1+leftSize,preEnd,it+1,inEnd);
return root;
}
};
二叉树遍历使用场景:
先序遍历:在第一次遍历到节点时就执行操作,一般只是想遍历执行操作(或输出结果)可选用先序遍历;
中序遍历:对于二分搜索树,中序遍历的操作顺序(或输出结果顺序)是符合从小到大(或从大到小)顺序的,故要遍历输出排序好的结果需要使用中序遍历
后序遍历:后续遍历的特点是执行操作时,肯定已经遍历过该节点的左右子节点,故适用于要进行破坏性操作的情况,比如删除所有节点
层次遍历用队列:
http://blog.csdn.net/le119126/article/details/49070317(判断二叉树是否平衡、是否完全二叉树、是否二叉排序树)