/**
* 代码复习
* 1.二叉树的创建+遍历+高度+结点数目
*
*/
#include <iostream>
#include <cstdio>
#include <vector>
#include <stack>
#include <algorithm>
#include <queue>
using namespace std;
struct TreeNode{
int val;
TreeNode* left;
TreeNode* right;
TreeNode():val(0), left(nullptr), right(nullptr){}
TreeNode(int x) : val(x), left(nullptr), right(nullptr){}
};
/**
* 创建二叉树
*/
//必须给出完全二叉树的结点数目:1 2 3 0 0 4 5 0 0 6 7 0 0 0 8 9 0 0 10 11 12 0 0 13 0 0 14 0 0
TreeNode* creatbtree(TreeNode* &root){
int num;
cin >> num;
if(num == 0){
return nullptr;
}else{
root = new TreeNode;
root->val = num;
creatbtree(root->left);
creatbtree(root->right);
}
return root;
}
//借助数组递归创建二叉树,没有限制
TreeNode* build(vector<int> &arr, int i){
if(arr[i] == -1 || i >= arr.size()){
return nullptr;
}
TreeNode* node = new TreeNode(arr[i]);
node->left = build(arr, 2*i+1);
node->right = build(arr,2*i+2);
return node;
}
/**
* 递归遍历二叉树
*/
//递归先序遍历二叉树
void preOrder(TreeNode* root, vector<int>& res){
if(root == nullptr){
return ;
}
res.push_back(root->val);
preOrder(root->left, res);
preOrder(root->right, res);
}
//递归中序遍历二叉树
void InOrder(TreeNode* root, vector<int> &res){
if(root == nullptr){
return ;
}
InOrder(root->left, res);
res.push_back(root->val);
InOrder(root->right, res);
}
//递归后序遍历二叉树
void PostOrder(TreeNode* root, vector<int> &res){
if(root == nullptr){
return ;
}
PostOrder(root->left, res);
PostOrder(root->right, res);
res.push_back(root->val);
}
/**
* 非递归遍历二叉树
*/
//非递归先序遍历二叉树--借助栈实现--一定先是右孩子先进栈,再是左孩子
vector<int> preOrderTraversal(TreeNode* root){
vector<int>result;//用来存放最后的遍历 结果
stack<TreeNode*> myStack;
if(root == nullptr){
return result;
}
myStack.push(root);
while(!myStack.empty()){
TreeNode* curr = myStack.top();
myStack.pop();
result.push_back(curr->val);
if(curr->right){
myStack.push(curr->right);
}
if(curr->left){
myStack.push(curr->left);
}
}
return result;
}
//非递归中序遍历二叉树
//借用指针的遍历来帮助访问节点,栈用来处理节点上的元素
//先一直向左走,直到叶子结点,该结点一定在栈的顶部,开始退栈,然后把该节点的右子树入栈,如果也没右子树里,继续看栈的顶部结点
vector<int> inOrderTraversal(TreeNode* root){
vector<int> result;//用来存储最终遍历结果
stack<TreeNode*> myStack;
if(root == nullptr){
return result;
}
TreeNode* current = root;//current遍历用的指针
while(current != NULL || !myStack.empty()){
if(current != NULL){
myStack.push(current);
current = current->left;//开始遍历左子树
}else{
current = myStack.top();//第一次时,是最左的叶子结点
result.push_back(current->val);
myStack.pop();
current = current->right;//开始右子树
}
}
return result;
}
//非递归后序遍历
//先序遍历的结果是中左右,调整代码改为中右左,再反转结果数组,变成了左右中
vector<int> postOrderTraversal(TreeNode* root){
stack<TreeNode*> myStack;
vector<int> result;
if(root == nullptr){
return result;
}
myStack.push(root);
while(!myStack.empty()){
TreeNode* curr = myStack.top();
myStack.pop();
result.push_back(curr->val);
if(curr->left) myStack.push(curr->left);
if(curr->right) myStack.push(curr->right);
}
reverse(result.begin(), result.end());//algorithm函数库
return result;
}
//数据翻转函数
void reverse1(vector<int> nums, int start, int end){
while(start < end){
swap(nums[start], nums[end]);
start += 1;
end -= 1;
}
}
//广度优先搜索BFS
vector<int> BFS(TreeNode* root){
vector<int> result;
queue<TreeNode*> queue;
if(root == nullptr){
return result;
}
queue.push(root);
while(!queue.empty()){
TreeNode* current = queue.front();
queue.pop();
result.push_back(current->val);
if(current->left) queue.push(current->left);
if(current->right) queue.push(current->right);
}
return result;
}
//深度优先搜索
vector<int> DFS(TreeNode* root){
vector<int> result;
stack<TreeNode*> stack;
if(root == nullptr){
return result;
}
stack.push(root);
while(!stack.empty()){
TreeNode* current = stack.top();
stack.pop();
result.push_back(current->val);
if(current->right) stack.push(current->right);
if(current->left) stack.push(current->left);
}
return result;
}
//递归深度优先搜索
void DFS2(TreeNode* root, vector<int> &result){
if(root == nullptr){
return ;
}else{
result.push_back(root->val);
DFS2(root->left, result);
DFS2(root->right, result);
}
}
//求二叉树的深度
int height(TreeNode* root){
if(root == nullptr){
return 0;
}else{
int lh = height(root->left);
int rh = height(root->right);
return 1+max(lh,rh);
//return 1+((lh>rh)?lh : rh);
}
}
//求出二叉树中叶子节点的个数
int blackNode(TreeNode* &root){
if(root == NULL)
return 0;
if(root->left == NULL && root->right == NULL)
return 1;
return blackNode(root->left) + blackNode(root->right);
}
//求二叉树结点总数
void sumNode(TreeNode* &root, int&sumnode){
if(root != NULL){
sumnode++;
sumNode(root->left, sumnode);
sumNode(root->right, sumnode);
}
}
//1 2 3 0 0 4 5 0 0 6 7 0 0 0 8 9 0 0 10 11 12 0 0 13 0 0 14 0 0
int main() {
vector<int> arr={1,2,3,-1,5,-1,7};
TreeNode* root;
root = build(arr, 0);
// root = creatbtree(root);
vector<int> res;
//递归遍历二叉树
// preOrder(root, res);
// InOrder(root, res);
// PostOrder(root,res);
//非递归遍历二叉树
//res = preOrderTraversal(root);//先序遍历
//res = inOrderTraversal(root);//中序遍历
//res = postOrderTraversal(root);//后序遍历
//res = BFS(root);//广度优先搜索
//res = DFS(root);//非递归深度优先搜索
//DFS2(root, res);//递归深度优先搜索
for(int i= 0; i < res.size(); i++){
printf("%d ",res[i]);
}
printf("\n");
return 0;
}
二叉树的创建(直接输入与数组)+ 二叉树的各种递归与非递归遍历+结点数目与深度
最新推荐文章于 2024-04-07 10:22:37 发布