当前位置:网站首页>Binary tree traversal - recursive and iterative templates
Binary tree traversal - recursive and iterative templates
2022-06-13 01:03:00 【Didi dada】
Foreword of binary tree 、 Middle preface 、 Iteration and recursion of subsequent traversal , One template, one catch
recursive
- The former sequence traversal
class Solution { public: vector<int> preorderTraversal(TreeNode* root) { vector<int> pre_res; preorder(root,pre_res); return pre_res; } void preorder(TreeNode* root ,vector<int> & res){ if(root == nullptr){ return; } res.emplace_back(root->val); preorder(root->left,res); preorder(root->right,res); } };
- In the sequence traversal
class Solution { public: void ldr(TreeNode* root, vector<int> & res ){ if (root==nullptr) return ; ldr(root->left , res); res.push_back(root -> val); ldr(root ->right , res); } vector<int> inorderTraversal(TreeNode* root) { vector<int> res; ldr(root,res); return res; } };
- After the sequence traversal
class Solution { public: vector<int> postorderTraversal(TreeNode* root) { vector<int> res; postorder(root,res); return res; } void postorder(TreeNode* root, vector<int>& res){ if(root==nullptr) return ; postorder(root->left,res); postorder(root->right,res); res.emplace_back(root->val); } };
iteration
- The former sequence traversal
class Solution { public: vector<int> postorderTraversal(TreeNode* root) { vector<int> res; postorder(root,res); return res; } void postorder(TreeNode* root, vector<int>& res){ if(root==nullptr) return ; postorder(root->left,res); postorder(root->right,res); res.emplace_back(root->val); } };
- In the sequence traversal
class Solution { public: vector<int> inorderTraversal(TreeNode* root) { vector<int> res; TreeNode* cur = root; stack<TreeNode*> s; while(s.size() || cur!=nullptr){ while(cur){ s.push(cur); cur = cur->left; } TreeNode* temp = s.top(); s.pop(); res.emplace_back(temp->val); cur =temp->right; } return res; } };
- After the sequence traversal
It is obtained by modifying the previous sequence during subsequent traversal :
Preorder traversal order : Around the middle ;
Exchange the left and right traversal order to get : Center right left ;
Output the result list in reverse order, i.e : Right and left ;
class Solution { public: vector<int> postorderTraversal(TreeNode* root) { vector<int> res; stack<TreeNode*> s; TreeNode* cur =root; while(s.size() || cur!=nullptr ){ while(cur){ res.emplace_back(cur->val); s.push(cur); cur = cur->right; } TreeNode* temp = s.top(); s.pop(); cur = temp->left; } reverse(res.begin(),res.end()); return res; } };
边栏推荐
- Kotlin coroutine suspend function suspend keyword
- [JS component] floating text
- [Latex] 插入圖片
- MCU serial port interrupt and message receiving and sending processing -- judge and control the received information
- Androi天气
- Jenkins持续集成操作
- 408 true question - division sequence
- [JS component] previous queue prompt
- 单片机串口中断以及消息收发处理——对接受信息进行判断实现控制
- 五篇经典好文,值得一看
猜你喜欢
redis
How to handle different types of data
Unity calls alertdialog
Common skills for quantitative investment - drawing 2: drawing the moving average
Illustrator tutorial, how to add dashes and arrows in illustrator?
Most elements leetcode
今日睡眠质量记录74分
[network protocol] problems and solutions in the use of LwIP
sort
What is meebits? A brief explanation
随机推荐
Characteristics of transactions -- atomicity (implementation principle)
[003] embedded learning: creating project templates - using stm32cubemx
Common skills of quantitative investment - index part 2: detailed explanation of BOL (Bollinger line) index, its code implementation and drawing
Hard (magnetic) disk (I)
Pipeline流水线项目构建
Et5.0 value type generation
[North Asia server data recovery] data recovery case of Hyper-V service paralysis caused by virtual machine file loss
Unitywebrequest asynchronous Download
[latex] insérer une image
Unity calls alertdialog
Win10 home vs pro vs enterprise vs enterprise LTSC
Several categories of software testing are clear at a glance
What is dummy change?
Binary tree -- using hierarchical sequence and middle sequence to determine a tree
五篇经典好文,值得一看(2)
Common skills for quantitative investment - indicators Chapter 3: detailed explanation of RSI indicators, their code implementation and drawing
[JS component] floating text
[JS component] customize the right-click menu
How to handle different types of data
[virtual machine] notes on virtual machine environment problems