美文网首页
二叉树的遍历(先序、中序、后序)

二叉树的遍历(先序、中序、后序)

作者: Source_Chang | 来源:发表于2020-10-09 14:28 被阅读0次

    树结构:

    struct BinaryTree {
    public:
        int value;
        BinaryTree *left;
        BinaryTree *right;
    };
    

    先序:
    递归:
    C++:

    void recursiveFrontEnumerateBinaryTreeImp(BinaryTree *root, std::vector<int>& results) {
        
        // 前序遍历
        // 根左右
        if ( !root ) {
            
            return;
        }
        
        results.push_back( root -> value );
        
        if ( root -> left ) {
            
            recursiveFrontEnumerateBinaryTreeImp( root -> left, results );
        }
        
        if ( root -> right ) {
            
            recursiveFrontEnumerateBinaryTreeImp( root -> right, results );
        }
    }
    
    
    std::vector<int> EnumerateBinaryTree::recursiveFrontEnumerateBinaryTree(BinaryTree *root) {
        
        std::vector<int> results;
        recursiveFrontEnumerateBinaryTreeImp( root, results );
        
        return results;
    }
    

    非递归:
    C++:

    std::vector<int> EnumerateBinaryTree::nonRecursiveFrontEnumerateBinaryTree(BinaryTree *root) {
        
        // 根左右
        std::vector<int> results;
        std::stack<BinaryTree *> stack;
        while ( root || !stack.empty() ) {
            
            while ( root ) {
                
                results.push_back( root -> value );
                stack.push( root );
                
                root = root -> left;
            }
            
            root = stack.top();
            stack.pop();
            root = root -> right;
        }
    
        return results;
    }
    

    中序:
    递归:
    C++:

    void recursiveMiddleEnumerateBinaryTreeImp(BinaryTree *root, std::vector<int>& results) {
        
        // 中序遍历
        // 左根右
        if ( !root ) {
            
            return;
        }
        
        if ( root -> left ) {
            
            recursiveMiddleEnumerateBinaryTreeImp( root -> left, results );
        }
        
        results.push_back( root -> value );
        
        if ( root -> right ) {
            
            recursiveMiddleEnumerateBinaryTreeImp( root -> right, results );
        }
    }
    
    
    std::vector<int> EnumerateBinaryTree::recursiveMiddleEnumerateBinaryTree(BinaryTree *root) {
        
        std::vector<int> results;
        recursiveMiddleEnumerateBinaryTreeImp( root, results );
        
        return results;
    }
    

    非递归:
    C++:

    std::vector<int> EnumerateBinaryTree::nonRecursiveMiddleEnumerateBinaryTree(BinaryTree *root) {
        
        std::vector<int> results;
        std::stack<BinaryTree *> stack;
        while ( root || !stack.empty() ) {
            
            while ( root ) {
                
                stack.push( root );
                root = root -> left;
            }
            
            root = stack.top();
            stack.pop();
            results.push_back( root -> value );
            root = root -> right;
        }
        
        return results;
    }
    

    后序:
    递归:
    C++:

    void recursiveBackEnumerateBinaryTreeImp(BinaryTree *root, std::vector<int>& results) {
        
        // 后序遍历
        // 左右根
        if ( !root ) {
            
            return;
        }
        
        if ( root -> left ) {
            
            recursiveBackEnumerateBinaryTreeImp( root -> left, results );
        }
        
        if ( root -> right ) {
            
            recursiveBackEnumerateBinaryTreeImp( root -> right, results );
        }
        
        results.push_back( root -> value );
    }
    
    
    std::vector<int> EnumerateBinaryTree::recursiveBackEnumerateBinaryTree(BinaryTree *root) {
        
        std::vector<int> results;
        recursiveBackEnumerateBinaryTreeImp( root, results );
        
        return results;
    }
    

    非递归:
    C++:

    std::vector<int> EnumerateBinaryTree::nonRecursiveBackEnumerateBinaryTree(BinaryTree *root) {
        
        // 左右根
        // 仿照先序遍历
        // 根右左
        // 最后再 reverse 就是 左右根 了
        std::vector<int> results;
        std::stack<BinaryTree *> stack;
        while ( root || !stack.empty() ) {
            
            while ( root ) {
                
                results.push_back( root -> value );
                
                stack.push( root );
                root = root -> right;
            }
            
            root = stack.top();
            stack.pop();
            root = root -> left;
        }
        std::reverse( results.begin(), results.end() );
        
        return results;
    }
    

    DEMO

    相关文章

      网友评论

          本文标题:二叉树的遍历(先序、中序、后序)

          本文链接:https://www.haomeiwen.com/subject/vwyauktx.html