算法与数据结构篇:二叉树的遍历及C++代码实现

#该文章代码参考慕课网——>玩转算法面试 从真题到思维全面提升算法思维#

二叉树的遍历方式

二叉树的遍历方式从大的方面可以分为两种:深度优先遍历和广度优先遍历。

深度优先搜索(Depth First Search),是沿着树的深度遍历树的节点,尽可能深的搜索树的分支,属于纵向遍历。

广度优先搜索(Breadth First Search),又叫宽度优先搜索或横向优先搜索,是从根结点开始沿着树的宽度搜索遍历。

具体遍历顺序区别,如下图:

鹏程朋诚

按照深度优先遍历有三种方式:分别为前序遍历、中序遍历、后序遍历。

前序遍历:根结点 ---> 左子树 ---> 右子树

结果为:ABDECFG

中序遍历:左子树---> 根结点 ---> 右子树

结果为:DBEAFCG

后序遍历:左子树 ---> 右子树 ---> 根结点

结果为:DEBFCGA

按照广度优先遍历有两种方式:从上至下的层序遍历、从下至上的层序遍历。

从上至下的层序遍历:从上至下,从左至右逐个节点遍历

结果为:ABCDEFG

从下至上的层序遍历:从下至上,从左至右逐个节点遍历

结果为:DEFGBCA

C++代码实现

1.二叉树的深度优先遍历

此处三个代码并非直接利用递归语句实现,而是采用了数据结构中的栈这种数据结构来模拟递归,实现二叉树的深度优先遍历。此处为该代码与众不同之处。

1.1 前序遍历

struct TreeNode {
       int val;
       TreeNode *left;
       TreeNode *right;
       reeNode(int x) : val(x), left(NULL), right(NULL) {}
  };
struct Command {
 	string command;
 	TreeNode* node;
 	Command(string s,TreeNode* n):command(s),node(n){}
 };
class Solution {
public:
    vector<int> preorderTraversal(TreeNode* root) {
      stack<Command> stack;
      vector<int> result;
	  if(root==NULL)
	  return result;
	  else{
	  	stack.push(Command("go",root));
	  	while(!stack.empty())
	  	{
	  		Command command=stack.top();
	  		stack.pop();
	  		if(command.command=="print")
	  		result.push_back(command.node->val);
	  		else
	  		{
	  			if(command.node->right)
	  			stack.push(Command("go",command.node->right));
	  			if(command.node->left)
	  			stack.push(Command("go",command.node->left));
	  			stack.push(Command("print",command.node));
			  }
		  }
		  return result;
	  } 
    }
};

1.2 中序遍历


struct TreeNode {
     int val;
     TreeNode *left;
     TreeNode *right;
     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  };
struct Command {
 	string command;
 	TreeNode* node;
 	Command(string s,TreeNode* n):command(s),node(n){}
 };
class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root) {
      stack<Command> stack;
      vector<int> result;
	  if(root==NULL)
	  return result;
	  else{
	  	stack.push(Command("go",root));
	  	while(!stack.empty())
	  	{
	  		Command command=stack.top();
	  		stack.pop();
	  		if(command.command=="print")
	  		result.push_back(command.node->val);
	  		else
	  		{
	  			if(command.node->right)
	  			stack.push(Command("go",command.node->right));
                stack.push(Command("print",command.node));
	  			if(command.node->left)
	  			stack.push(Command("go",command.node->left));
	  			
			  }
		  }
		  return result;
	  } 
    }
};

1.3 后序遍历

struct TreeNode {
     int val;
     TreeNode *left;
     TreeNode *right;
     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 };
struct Command {
 	string command;
 	TreeNode* node;
 	Command(string s,TreeNode* n):command(s),node(n){}
 };
class Solution {
public:
    vector<int> postorderTraversal(TreeNode* root) {
      stack<Command> stack;
      vector<int> result;
	  if(root==NULL)
	  return result;
	  else{
	  	stack.push(Command("go",root));
	  	while(!stack.empty())
	  	{
	  		Command command=stack.top();
	  		stack.pop();
	  		if(command.command=="print")
	  		result.push_back(command.node->val);
	  		else
	  		{
	  			stack.push(Command("print",command.node));
                if(command.node->right)
	  			stack.push(Command("go",command.node->right));
	  			if(command.node->left)
	  			stack.push(Command("go",command.node->left));
	  			
			  }
		  }
		  return result;
	  } 
    }
};

2.二叉树的广度优先遍历

2.1 从上至下层序遍历

struct TreeNode {
     int val;
     TreeNode *left;
     TreeNode *right;
     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 };
class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
     vector<vector<int>> result;
	 queue<pair<int,TreeNode*> > queue;
	 queue.push(make_pair(0,root));
	 if(root==NULL)
	 return result;
	 else{
	 	while(!queue.empty()){
	 	int level=queue.front().first;
	 	TreeNode* node=queue.front().second;
	 	queue.pop();
	 	if(level==result.size())
	 	result.push_back(vector<int>());
	 	result[level].push_back(node->val);
	 	if(node->left)
	 	queue.push(make_pair(level+1,node->left));
	 	if(node->right)
	 	queue.push(make_pair(level+1,node->right));
		 }
	 }
	 return result;
    }
};

2.2 从下至上层序遍历

struct TreeNode {
     int val;
    TreeNode *left;
     TreeNode *right;
     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 };
class Solution {
public:
    vector<vector<int>> levelOrderBottom(TreeNode* root) {
     deque<vector<int>> result1;
	 queue<pair<int,TreeNode*> > queue;
	 queue.push(make_pair(0,root));
	 if(root==NULL)
	 return vector<vector<int>>();
	 else{
	 	while(!queue.empty()){
	 	int level=queue.front().first;
	 	TreeNode* node=queue.front().second;
	 	queue.pop();
	 	if(level==result1.size())
	 	result1.push_front(vector<int>());
	 	result1[0].push_back(node->val);
	 	if(node->left)
	 	queue.push(make_pair(level+1,node->left));
	 	if(node->right)
	 	queue.push(make_pair(level+1,node->right));
		 }
	 }
	 return vector<vector<int>>(result1.begin(),result1.end());
    }
};

 

猜你喜欢

转载自blog.csdn.net/qq_36163358/article/details/84938059