树的非递归遍历

Posted WayToAccept

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了树的非递归遍历相关的知识,希望对你有一定的参考价值。

1.先序(我只把这些当模板)

class Solution 
public:
    vector<int> preorderTraversal(TreeNode* root) 
        vector<int> res;
        if(root==NULL)return res;
        stack<TreeNode*> ms;
        TreeNode* p=root;
        while(!ms.empty()||p)
        
            if(p)
            
                res.push_back(p->val);
                ms.push(p);
                p=p->left;
            
            else if(!ms.empty())
            
                p=ms.top();
                ms.pop();
                if(p)p=p->right;
            
        
        return res;
    
;


2.中序

class Solution 
public:
    vector<int> inorderTraversal(TreeNode* root) 
        vector<int> res;
        if(root==NULL)return res;
        stack<TreeNode*> ms;
        TreeNode* p=root;
        while(!ms.empty()||p)
        
            if(p)
            
                ms.push(p);
                p=p->left;
            
            else
            
                p=ms.top();
                res.push_back(p->val);
                ms.pop();
                p=p->right;
            
        
        return res;
    
;



3.后序

class Solution 
public:
    vector<int> postorderTraversal(TreeNode* root) 
        vector<int> res;
        if(root==NULL)return res;
        stack<TreeNode*> ms;
        TreeNode *p=root,*pre=NULL;
        while(!ms.empty()||p)
        
            if(p)
            
                ms.push(p);
                p=p->left;
            
            else
            
                p=ms.top();
                if(p->right!=NULL&&p->right!=pre)p=p->right;
                else 
                
                    res.push_back(p->val);
                    ms.pop();
                    pre=p;
                    p=NULL;
                
            
        
        return res;
    
;




看看大神的代码(以下代码来源于leetcode)

1.先序(看起来很优美)

class Solution 
public:
vector<int> preorderTraversal(TreeNode *root) 
    if (root==NULL) 
        return vector<int>();
    
    vector<int> result;
    stack<TreeNode *> treeStack;
    treeStack.push(root);
    while (!treeStack.empty()) 
        TreeNode *temp = treeStack.top();
        result.push_back(temp->val);
        treeStack.pop();
        if (temp->right!=NULL) 
            treeStack.push(temp->right);
        
        if (temp->left!=NULL) 
            treeStack.push(temp->left);
        
    
    return result;

;



2.中序( o(1)空间复杂度)

Morris traversal

 
 

public class TreeNode 
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode(int x)  val = x; 


public class Solution 
    public List<Integer> inorderTraversal(TreeNode root) 
        if(root == null) return new ArrayList<Integer>();
        List<Integer> res = new ArrayList<Integer>();
        TreeNode pre = null;
        while(root != null)
        	if(root.left == null)
        		res.add(root.val);
        		root = root.right;
        	else
        		pre = root.left;
        		while(pre.right != null && pre.right != root)
        			pre = pre.right;
        		
        		if(pre.right == null)
        			pre.right = root;
        			root = root.left;
        		else
        			pre.right = null;
        			res.add(root.val);
        			root = root.right;
        		
        	
        
        return res;
    


3.后序(美得不敢直视)

vector<int> postorderTraversal(TreeNode *root) 
    vector<int> v;
    if (!root) return v;
    
    stack<TreeNode *> s;
    s.push(root);
    
    TreeNode *p = NULL;
    while(!s.empty()) 
        p = s.top();
        s.pop();
        v.insert(v.begin(), p->val);
        if (p->left) s.push(p->left);
        if (p->right) s.push(p->right);
    
    
    return v;




以上是关于树的非递归遍历的主要内容,如果未能解决你的问题,请参考以下文章

最简方式实现二叉树的非递归遍历

树的非递归遍历

二叉树的非递归遍历

树的非递归遍历——前序中序后序

二叉树的非递归遍历

二叉树的非递归遍历