当前位置:网站首页>To remember the composition ~ the pre order traversal of binary tree
To remember the composition ~ the pre order traversal of binary tree
2022-04-23 09:01:00 【Xiaoqiang~】
Writing to remember ~ Preorder traversal of two tree
0、 Preface
This blog is to buckle 144. Preorder traversal of two tree Problem solving , The title is simple. , Blogging is mainly because it can also be used as DFS Examples ~
And this blog is actually related to Writing to remember ~ Middle order traversal of binary trees The implementation of middle order traversal in is almost , Can compare memory ~
The middle order traversal of binary tree can Click here to To view the !
The post order traversal of binary tree can Click here to To view the !
1、 Title Description

2、 Their thinking
2.1 Method 1 ~ recursive
2.1.1 Ideas
The preorder traversal of a binary tree is based on access The root node – The left subtree – Right subtree Traverse the target tree in a way , When accessing the left subtree or right subtree, you can traverse in the same way , Until you walk through the whole tree . In combination with this approach, recursion can be used directly !
2.1.2 Program code
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */
class Solution {
public:
void preorder(TreeNode* root,vector<int>& ans)
{
if(!root)
return;
ans.push_back(root->val);
preorder(root->left,ans);
preorder(root->right,ans);
}
vector<int> preorderTraversal(TreeNode* root) {
vector<int> ans;
preorder(root,ans);
return ans;
}
};
2.2 Method 2 ~ DFS
2.2.1 Ideas
What about this idea , Is based on the force given on the buckle DFS Templates 2 Written by . Although it feels more like iteration , harm !
The idea here is equivalent to the method 1 The train of thought unfolds , From implicit stack to explicit stack , Everything else is the same , The specific implementation can be seen in the following code .
2.2.2 Program code
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */
class Solution {
public:
vector<int> preorderTraversal(TreeNode* root) {
vector<int> ans;
TreeNode* cur = root;
stack<TreeNode*> st;
while(cur || !st.empty())
{
while(cur)
{
st.push(cur);
ans.push_back(cur->val);
cur = cur->left;
}
cur = st.top();
st.pop();
cur=cur->right;
}
return ans;
}
};
版权声明
本文为[Xiaoqiang~]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230726592682.html
边栏推荐
猜你喜欢

Consensus Token:web3. 0 super entrance of ecological flow

DJ music management software pioneer DJ rekordbox

php基于哈希算法出现的强弱比较漏洞

Please arrange star trek in advance to break through the new playing method of chain tour, and the market heat continues to rise

企业微信应用授权/静默登录

L2-022 重排链表 (25 分)(map+结构体模拟)

Enterprise wechat application authorization / silent login

Latex paper typesetting operation

What is augmented reality technology? Where can it be used?

请提前布局 Star Trek突破链游全新玩法,市场热度持续高涨
随机推荐
LeetCode396. Rotate array
Complete binary search tree (30 points)
Solidity 问题汇总
搜索树判断 (25 分)
The most concerned occupations after 00: civil servants ranked second. What was the first?
How does kubernetes use harbor to pull private images
完全二叉搜索树 (30 分)
还原二叉树 (25 分)
bashdb下载安装
Notes on xctf questions
2021李宏毅机器学习之Adaptive Learning Rate
Wechat: get the owner of a single tag
On time atom joins hands with oneos live broadcast, and the oneos system tutorial is fully launched
Latex mathematical formula
Brush classic topics
2021 Li Hongyi's adaptive learning rate of machine learning
调包求得每个样本的k个邻居
Summary of solid problems
The K neighbors of each sample are obtained by packet switching
LeetCode396.旋转数组