二叉树按层输入及先序输入,及三种输出

二叉树的指针和地址看的真难受,各种问题。
*&

#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include "string.h"
#define num 100
using namespace std;
typedef char datatype;
typedef struct  BinTNode{
   
     BinTNode *lchild;
     BinTNode *rchild;
    char data;
}BinTNode,*BinTree;

//按层输入 ABC@D@F@@E#
void CreateBinTree(BinTree &bt)
{
   
    BinTNode *Q[num];
BinTNode *s;
int parent,child;
char ch;
ch=getchar();
bt=NULL;
parent=1;
child=0;
  while(ch!=
假设二叉树的节点结构体为: ```c++ struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} }; ``` 我们可以使用递归的方式实现先序、中序、后序遍历。具体实现如下: ```c++ 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 << " "; } ``` 其中,先序遍历先访问根节点,然后访问左子树、右子树;中序遍历先访问左子树,然后访问根节点、右子树;后序遍历先访问左子树,然后访问右子树、根节点。 对于按照层次序列输入二叉树,我们可以使用队列来实现。具体实现如下: ```c++ TreeNode* buildTree(vector<int>& nums) { if (nums.empty()) return nullptr; TreeNode* root = new TreeNode(nums[0]); queue<TreeNode*> q{{root}}; int i = 1; while (!q.empty() && i < nums.size()) { TreeNode* curr = q.front(); q.pop(); if (i < nums.size() && nums[i] != -1) { curr->left = new TreeNode(nums[i]); q.push(curr->left); } i++; if (i < nums.size() && nums[i] != -1) { curr->right = new TreeNode(nums[i]); q.push(curr->right); } i++; } return root; } ``` 其中,-1 表示节点为空。使用一个队列来存储每层的节点,对于每个节点,如果其左(右)子节点存在,则在队列中插入该节点的左(右)子节点。最后返回根节点即可。 完整代码如下: ```c++ #include <iostream> #include <vector> #include <queue> using namespace std; struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} }; TreeNode* buildTree(vector<int>& nums) { if (nums.empty()) return nullptr; TreeNode* root = new TreeNode(nums[0]); queue<TreeNode*> q{{root}}; int i = 1; while (!q.empty() && i < nums.size()) { TreeNode* curr = q.front(); q.pop(); if (i < nums.size() && nums[i] != -1) { curr->left = new TreeNode(nums[i]); q.push(curr->left); } i++; if (i < nums.size() && nums[i] != -1) { curr->right = new TreeNode(nums[i]); q.push(curr->right); } i++; } return root; } 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{1, 2, 3, -1, 4, 5, 6}; TreeNode* root = buildTree(nums); cout << "preOrder: "; preOrder(root); cout << endl; cout << "inOrder: "; inOrder(root); cout << endl; cout << "postOrder: "; postOrder(root); cout << endl; return 0; } ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值