๐Ÿช
cookielau
  • Introduction
  • Machine Learning
    • Distributed
      • Bookmarks
    • NLP
      • Transformers
    • MLC
      • Tensor Program Abstraction
      • End-to-End Module Execution
  • Framework
    • PyTorch
      • Bookmarks
      • Model
      • Shared
      • Miscellaneous
    • Tensorflow
      • Bookmarks
      • Model
      • Shared
      • Miscellaneous
    • CUDA
      • Bookmarks
    • DeepSpeed
    • Bagua
      • Model
      • Optimizer
    • Others
      • Bookmarks
  • About Me
    • 2022-04-28
  • Random Thoughts
  • Archives
    • CPP
      • Bookmarks
      • Container
      • Algorithm
      • FILE CONTROL
      • Virtual Table
      • Assembly
      • Key Words
      • Problems
      • Others
    • JAVA
      • String Container
      • Maps
    • PYTHON
      • Bookmarks
      • Python Tools
        • Batch Rename
        • Combine Excel
        • Excel Oprations
        • Read Write Excel
        • Rotate PDF
      • Library
        • Pandas Notes
        • Numpy Notes
        • Json Notes
      • Spider
        • Selenium Install
        • Selenium Locating
        • Selenium Errors
        • Selenium Basics
      • Django
        • Start Up
      • Others
    • LINUX
      • Installation
      • Cli Tools
      • WSL
      • Bugs
    • JUNIOR2
      • Economics
        • Chapter 0x01 ็ปๆตŽ็ฎก็†ๆฆ‚่ฟฐ
        • Chapter 0x02 ๅพฎ่ง‚ๅธ‚ๅœบๆœบๅˆถๅˆ†ๆž
        • Chapter 0x03 ็”Ÿไบงๅ†ณ็ญ–ไธŽๅธ‚ๅœบ็ป“ๆž„
        • Chapter 0x04 ๅฎ่ง‚็ปๆตŽๅธ‚ๅœบๅˆ†ๆž
        • Chapter 0x05 ็ฎก็†็š„่Œ่ƒฝ
        • Chapter 0x06 ็”Ÿไบง็ณป็ปŸ็ป“ๆž„ไธŽๆˆ˜็•ฅ
        • Chapter 0x0b ๆŠ•่ต„้กน็›ฎ็ปๆตŽ่ฏ„ไปท
        • Chapter 0x0f ๆŠ•่ต„้กน็›ฎ็ปๆตŽ่ฏ„ไปท
      • Computer Network
        • ๆฆ‚่ฟฐ
        • ๅˆ†ๅฑ‚ๆจกๅž‹
        • ็‰ฉ็†ๅฑ‚
        • ๆ•ฐๆฎ้“พ่ทฏๅฑ‚
        • ็ฝ‘็ปœๅฑ‚
        • ไผ ่พ“ๅฑ‚
        • ๅบ”็”จๅฑ‚
        • HTTP(s)ๅฎž้ชŒ
        • [Practice]
      • Software Engineering
        • Introduction
        • Demand Analysis
        • Task Estimation
        • Presentation
      • Network Security
        • Chapter 0x01 ๆฆ‚่ฟฐ
        • Chapter 0x02 ๅฏ†็ ๅญฆ
        • Chapter 0x03 ๅ…ฌ้’ฅไฝ“ๅˆถ
        • Chapter 0x04 ๆถˆๆฏ่ฎค่ฏ
        • Chapter 0x05 ๅฏ†้’ฅ็ฎก็†
        • Chapter 0x06 ่ฎฟ้—ฎๆŽงๅˆถ
        • Assignments
      • x86 Programming
        • Basic Knowledge
        • Program Design
        • System Interruption
        • Frequently used functions
    • MD&LaTex
      • Markdown
      • LaTex
    • NPM
      • NPM LINK
    • MyBlogs
      • 2020BUAA่ฝฏๅทฅโ€”โ€”โ€œๅœไธ‹ๆฅ๏ผŒๅ›žๅคด็œ‹โ€
      • 2020BUAA่ฝฏๅทฅโ€”โ€”โ€œๅˆ็ชฅๆž„ๅปบไน‹ๆณ•โ€
      • 2020BUAA่ฝฏๅทฅโ€”โ€”โ€œไธŠๆ‰‹่ฝฏไปถๅทฅ็จ‹๏ผŒPSPๅˆไฝ“้ชŒ๏ผโ€
      • 2020BUAA่ฝฏๅทฅโ€”โ€”โ€œๆทฑๅบฆ่ฏ„ๆต‹ๅฎ˜โ€
      • 2020BUAA่ฝฏๅทฅโ€”โ€”โ€œๅนถ่‚ฉไฝœๆˆ˜๏ผŒๅนณ้ขไบค็‚นProโ€
    • SC
      • PAC 2022
        • Lectures
      • OpenMP & MPI
        • MPI Overview
        • Message Passing Programming
        • OpenMP Overview
        • Work Sharing Directives
        • Annual Challenge
        • Future Topics in OpenMP
        • Tasks
        • OpenMP & MPI
    • Hardware
      • Nvidia GPU
        • Frequent Error
        • Memory Classification
        • CUDA_7_Streams_Simplify_Concurrency
        • Optimize_Data_Transfers_in_CUDA
        • Overlap_Data_Transfers_in_CUDA
        • Write_Flexible_Kernels_with_Grid-Stride_Loops
        • How_to_Access_Global_Memory_Efficiently
        • Using_Shared_Memory
      • Intel CPU
        • Construction
        • Optimization
        • Compilation
        • OpenMP
    • English
      • Vocab
      • Composition
    • Interview
      • Computer Network
Powered by GitBook
On this page
  • Binary Tree iteration
  • DFS & BFS

Was this helpful?

  1. Archives
  2. CPP

Algorithm

Binary Tree iteration

้€’ๅฝ’้ๅކ

//ๅ‰ๅบ้ๅކ
void preorder(TreeNode *root, vector<int> &path)
{
    if(root != NULL)
    {
        path.push_back(root->val);
        preorder(root->left, path);
        preorder(root->right, path);
    }
}

//ไธญๅบ้ๅކ
void inorder(TreeNode *root, vector<int> &path)
{
    if(root != NULL)
    {
        inorder(root->left, path);
        path.push_back(root->val);
        inorder(root->right, path);
    }
}

//ๅŽ็ปญ้ๅކ
void postorder(TreeNode *root, vector<int> &path)
{
    if(root != NULL)
    {
        postorder(root->left, path);
        postorder(root->right, path);
        path.push_back(root->val);
    }
}

้ž้€’ๅฝ’้ๅކไบŒๅ‰ๆ ‘ๅฎž้™…ไธŠๅฐฑๆ˜ฏ็œ‹ๆ˜ฏๅฆ็ฌฌไบŒๆฌก้ๅކ๏ผŒ็ฌฌไบŒๆฌก้ๅކ็š„ๆ—ถๅ€™ๅŠ ๅ…ฅๅˆฐ็ป“ๆžœ้›†ๅˆไธญๅณๅฏ๏ผŒๆŒ‰็…ง่ฟ™ไธชๆ€ๆƒณๅฐฑๅฏไปฅๅ€ŸๅŠฉstack็š„ๅ…ˆ่ฟ›ๅ…ˆๅ‡บๆŽงๅˆถๆ ‘็š„็ป“็‚น็š„้ๅކ๏ผš

//ๆ›ด็ฎ€ๅ•็š„้ž้€’ๅฝ’ๅ‰ๅบ้ๅކ
void preorderTraversalNew(TreeNode *root, vector<int> &path)
{
    stack< pair<TreeNode *, bool> > s;
    s.push(make_pair(root, false));
    bool visited;
    while(!s.empty())
    {
        root = s.top().first;
        visited = s.top().second;
        s.pop();
        if(root == NULL)
            continue;
        if(visited)
        {
            path.push_back(root->val);
        }
        else
        {
            s.push(make_pair(root->right, false));
            s.push(make_pair(root->left, false));
            s.push(make_pair(root, true));
        }
    }
}

//ๆ›ด็ฎ€ๅ•็š„้ž้€’ๅฝ’ไธญๅบ้ๅކ
void inorderTraversalNew(TreeNode *root, vector<int> &path)
{
    stack< pair<TreeNode *, bool> > s;
    s.push(make_pair(root, false));
    bool visited;
    while(!s.empty())
    {
        root = s.top().first;
        visited = s.top().second;
        s.pop();
        if(root == NULL)
            continue;
        if(visited)
        {
            path.push_back(root->val);
        }
        else
        {
            s.push(make_pair(root->right, false));
            s.push(make_pair(root, true));
            s.push(make_pair(root->left, false));
        }
    }
}

//ๆ›ด็ฎ€ๅ•็š„้ž้€’ๅฝ’ๅŽๅบ้ๅކ
void postorderTraversalNew(TreeNode *root, vector<int> &path)
{
    stack< pair<TreeNode *, bool> > s;
    s.push(make_pair(root, false));
    bool visited;
    while(!s.empty())
    {
        root = s.top().first;
        visited = s.top().second;
        s.pop();
        if(root == NULL)
            continue;
        if(visited)
        {
            path.push_back(root->val);
        }
        else
        {
            s.push(make_pair(root, true));
            s.push(make_pair(root->right, false));
            s.push(make_pair(root->left, false));
        }
    }
}

DFS & BFS

https://leetcode-cn.com/problems/binary-tree-level-order-traversal/solution/bfs-de-shi-yong-chang-jing-zong-jie-ceng-xu-bian-l/

PreviousContainerNextFILE CONTROL

Last updated 2 years ago

Was this helpful?

้ž้€’ๅฝ’้ๅކ Reference:

ๆ›ด็ฎ€ๅ•็š„้ž้€’ๅฝ’้ๅކไบŒๅ‰ๆ ‘็š„ๆ–นๆณ•