赞
踩
对于二叉树有两种搜索方式:深度优先搜索与广度优先搜索,而深度优先搜索分为前序,中序,后序三种遍历方式,下面给几种遍历方式一 一解释。
对于下面二叉树的三种遍历顺序:
对于这三种遍历都给出递归与非递归的方式:(下面所有的举例都是使用下图二叉树)
对于递归的方式,代码如下:
前序遍历(递归)
- class solution{
- public:
- vector<int> result;
- public:
- vector<int> Preorder_traversal(TreeNode *root)
- {
- //vector<int> result;
- if(root!=null)
- {
- result.push_back(root->data);
- if(root->left)
- {
- Preorder_traversal(root->left);
- }
- if(root->right)
- {
- Preorder_traversal(root->right);
- }
- }
- return result;
- }
- }

中序遍历(递归)
- class solution{
- public:
- vector<int> result;
- public:
- vector<int> Inorder_traversal(TreeNode *root)
- {
- if(root!=null)
- {
- if(root->left)
- {
- Inorder_traversal(root->left);
- }
- result.push_back(root);
- if(root->right)
- {
- Inorder_traversal(root->right);
- }
- }
-
- }
- }

后续遍历(递归)
- class solution{
- public:
- vector<int> result;
- public:
- vector<int> Subsequent_traversal(TreeNode *root)
- {
- if(root->left)
- {
- Subsequent_traversal(root->left);
- }
- if(root->right)
- {
- Subsequent_traversal(root->right);
- }
- result.push_back(root);
-
- }
- }

而对于非递归方式,要借助于栈来实现:
- /*前序搜索*/
- class solution{
- public:
- vector<int> result;
- public:
- vector<int> Preorder_traversal(TreeNode *root)
- {
- if(root==null)
- return ;
-
- stack<TreeNode*> pre_stack=new stack<>;
- pre_stack.push(root);
-
- while(!pre_stack.empty)
- {
- stack<TreeNode*> temp=pre_stack.front();
- result.push_back(pre_stack.top()->data);
- pre_stack.pop();
- if(temp->right!=null)
- {
- pre_stack.push(temp->right);
- }
- if(temp->left!=null)
- {
- pre_stack.push(temp->left);
- }
- }
- }
- }

步骤 | 压入数据 | 下一步操作情况 | 弹出数据 | 下一步操作情况 | 栈中数据 |
1 | 8 | 有左节点,继续压入 | 8 | ||
2 | 7 | 有左节点,继续压入 | 8、7 | ||
3 | 5 | 无左节点,停止压入 | 8、7、5 | ||
4 | 5 | 无右节点,继续弹出 | 8、7 | ||
5 | 7 | 有右节点,停止弹出 | 8 | ||
6 | 20 | 无左节点,停止压入 | 8、20 | ||
7 | 20 | 无右节点,继续弹出 | 8 | ||
8 | 8 | 有右节点,停止弹出 | 无数据 | ||
9 | 9 | 有左节点,继续压入 | 9 | ||
10 | 13 | 无左节点,停止压入 | 9、13 | ||
11 | 13 | 无右节点,继续弹出 | 9 | ||
12 | 9 | 有右节点,停止弹出 | 无 | ||
13 | 14 | 无左节点,停止压入 | 14 | ||
14 | 14 | 无右节点,且为空(两者均满足,遍历完,停止) | 无 |
- /*中序搜索*/
- class solution{
- public:
- vector<int> result;
- public:
- vector<int> Inorder_traversal(TreeNode *root)
- {
- if(root==null)
- return;
- }
-
- stack<TreeNode*> Inorder_stack;
- TreeNode* p;
- p=root;
- while(p||!Inorder_stack.empty())
- {
- if(p)
- {
- Inorder_stack.push(p);
- p=p->left;
- }
- else
- {
- p=Inorder_stack.top();
- result.push_back(p->data);
- Inorder_stack.pop();
- p=p->right;
- }
-
- }
- return result;
- }

- /*后续搜索*/
- class solution
- {
- public:
- vector<int> result;
- public:
- vector<int> Subsequent_traversal(TreeNode*root)
- {
- if(root==null)
- return;
- stack<TreeNode*> Subsequent_stack;
- TreeNode* p=root;
- Subsequent_stack.push(p);
- Subsequent_stack.push(p);
- while(!Subsequent_stack.empty)
- {
- p=Subsequent_stack.top();//获得栈顶数据的指针
- Subsequent_stack.pop();//弹出栈顶数据
- if(p->data==Subsequent_stack.top()->data)//判断弹出栈顶数据之前,前后两个数据是否相等
- { //如果右子节点存在,先压入右子节点,后压入左子结点
- if(p->right)
- {
- Subsequent_stack.push(p->right);
- Subsequent_stack.push(p->right);
- }
- if(p->left)
- {
- Subsequent_stack.push(p->left);
- Subsequent_stack.push(p->left);
- }
- }
- else
- {
- result->push_back(p->data);
- }
- }
- return result;
- }
- }

未完待续.......
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。