二叉树.1.遍历

Posted 工程师Ruojhen

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了二叉树.1.遍历相关的知识,希望对你有一定的参考价值。

Binary-Tree

本篇文章为[`shaoyuanhangyes.github.io/二叉树`]的重置版本 

更新了部分代码 更改了排版 舍弃了冗杂的赘述内容 利用STL容器进行了更新


二叉树



二叉树结构体


struct TreeNode{ int val; TreeNode *left; TreeNode *right; TreeNode(int x):val(x),left(NULL),right(NULL) {}};



二叉树的创建

采用的是更为直观的层序遍历的方式 将二叉树和对应数组序号的元素一一填入


//以层序的方式创建二叉树 len为数组长度 index为元素位置序号TreeNode* createTree(const vector<int> &nums,int len,int index){ TreeNode *root=NULL; if(index<len&&nums[index]!=-1){//值为null和位置不合法时直接返回空节点 root = new TreeNode(nums[index]); root->left = createTree(nums,len,2*index+1); root->right= createTree(nums,len,2*index+2); } return root;}


二叉树的遍历

二叉树的遍历中 层序遍历属于广度优先(BFS) 前中后序遍历都属于深度优先(DFS)


层序遍历

利用队列进行迭代


//层序遍历 利用队列进行迭代 返回的是一个二维vector数组 因此需要一个函数来打印这个vector数组vector<vector<int>> levelOrder(TreeNode* root) { vector<vector<int>> res; if(!root) return res; queue<TreeNode* > Tree; Tree.push(root); while(!Tree.empty()){ vector<int> temp; int len=Tree.size(); while(len--){ TreeNode *pNode=Tree.front(); Tree.pop(); temp.push_back(pNode->val); if(pNode->left) Tree.push(pNode->left); if(pNode->right) Tree.push(pNode->right); } res.push_back(temp); } return res;}


打印二维数组的代码为


//打印二维数组void PrintMartrix(vector<vector<int>>& res){ for(int i=0;i<res.size();++i){ cout<<"["; for(int j=0;j<res[i].size();++j){ cout<<" "<<res[i][j]<<" "; } cout<<"]"<<endl; }}



先序遍历

遍历顺序为 `根 左 右`


//先序遍历递归算法 遍历到就直接cout输出void preOrder(TreeNode *root){ if(!root) return; cout<<root->val<<" "; preOrder(root->left); preOrder(root->right);}



中序遍历

遍历顺序为 `左 根 右`


//中序遍历递归算法void inOrder(TreeNode *root){ if(!root) return; inOrder(root->left); cout<<root->val<<" "; inOrder(root->right);}



后序遍历

遍历顺序为 `左 右 根`


//后序遍历递归算法void postOrder(TreeNode *root){ if(!root) return; postOrder(root->left); postOrder(root->right); cout<<root->val<<" ";}




全部代码


#include <iostream>#include <vector>#include <queue>using namespace std;struct TreeNode{ int val; TreeNode *left; TreeNode *right; TreeNode(int x):val(x),left(NULL),right(NULL) {}};//以层序的方式创建二叉树 len为数组长度 index为元素位置序号TreeNode* createTree(const vector<int> &nums,int len,int index){ TreeNode *root=NULL; if(index<len&&nums[index]!=-1){//值为null和位置不合法时直接返回空节点 root = new TreeNode(nums[index]); root->left = createTree(nums,len,2*index+1); root->right= createTree(nums,len,2*index+2); } return root;}//打印二维数组void PrintMartrix(vector<vector<int>>& res){ for(int i=0;i<res.size();++i){ cout<<"["; for(int j=0;j<res[i].size();++j){ cout<<" "<<res[i][j]<<" "; } cout<<"]"<<endl; }}//层序遍历 利用队列进行迭代vector<vector<int>> levelOrder(TreeNode* root) { vector<vector<int>> res; if(!root) return res; queue<TreeNode* > Tree; Tree.push(root); while(!Tree.empty()){ vector<int> temp; int len=Tree.size(); while(len--){ TreeNode *pNode=Tree.front(); Tree.pop(); temp.push_back(pNode->val); if(pNode->left) Tree.push(pNode->left); if(pNode->right) Tree.push(pNode->right); } res.push_back(temp); } return res;}//先序遍历递归算法void preOrder(TreeNode *root){ if(!root) return; cout<<root->val<<" "; preOrder(root->left); preOrder(root->right);}//中序遍历递归算法void inOrder(TreeNode *root){ if(!root) return; inOrder(root->left); cout<<root->val<<" "; inOrder(root->right);}//后序遍历递归算法void postOrder(TreeNode *root){ if(!root) return; postOrder(root->left); postOrder(root->right); cout<<root->val<<" ";}

int main(){ vector<int> nums={0,1,2,3,4,5,6,7,8,-1,10};//示例 -1代表所在位置为空值 int len=nums.size(); TreeNode *root=createTree(nums,len,0); cout<<"层序遍历的二维数组序列为: "<<endl; vector<vector<int>> res=levelOrder(root); PrintMartrix(res); cout<<endl<<"先序遍历序列为: "; preOrder(root); cout<<endl<<"中序遍历序列为: "; inOrder(root); cout<<endl<<"后序遍历序列为: "; postOrder(root); return 0;}



程序输出结果为


层序遍历的二维数组序列为:[ 0 ][ 1 2 ][ 3 4 5 6 ][ 7 8 10 ]

先序遍历序列为: 0 1 3 7 8 4 10 2 5 6中序遍历序列为: 7 3 8 1 4 10 0 5 2 6后序遍历序列为: 7 8 3 10 4 1 5 6 2 0Process finished with exit code 0


工程师Ruojhen推荐搜索