二叉树的层次和先中后序遍历c++实现

//层次遍历 栈的应用
class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
         vector<vector<int>> res;
        if (!root) {
            return res;
        }
        queue<TreeNode*> q;
        q.push(root);
        while (!q.empty()){
            int currentLevelSize = q.size();
            res.push_back(vector<int>());
            for (int i = 1; i <= currentLevelSize; i++) {
                auto node = q.front();
                q.pop();
                res.back().push_back(node->val);
                if (node->left)
                    q.push(node->left);
                if (node->right)
                    q.push(node->right);
            }
        }
        return res;
    }
};
//先序遍历 递归
struct TreeNode{
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
};

void preorder(struct TreeNode* root, int *res, int* resSize)
{
    if(root == NULL)
    {
        return;
    }
    res[*resSieze++] = root->val;
    preorder(root->left,res,resSize);
	preorder(root->right,res,resSize);
}

int preorderTraversal(struct TreeNode* root,int* returnSize){
	int *res = malloc(sizeof(int)*200);
	*returnSize=0;
	preorder(root,res,returnSize);
	return res;
}

//迭代法
class Solution {
public:
     vector<int> preorderTraversal(TreeNode* root) {
        vector<int> res;
        if (root == NULL) {
            return res;
        }
        stack<TreeNode*> stk;
        TreeNode* node = root;
        while (!stk.empty() || node != NULL) {
            //此循环完成头节点和左节点的值插入
            while (node != NULL) {
                //根节点的值插入
                res.emplace_back(node->val);
                //将链表头节点插入栈
                stk.emplace(node);
                //将节点换到下一个左节点(可能是下一个头节点)
                node = node->left;
            }
            node = stk.top();
            stk.pop();
            //有右节点会执行第一个循环(又是头节点)
            node = node->right;
        }
        return res;
    }
};
//中序遍历
void inorder(struct TreeNode* root,int* res,int* returnSize){
    if(root==NULL){
		return;
    }
    //先左再根
    inorder(root->left,res,returnSize);
    res[(*returnSize)++] = root->val;
    inorder(root->right,res,returnSize);
 }
int* inorderTraversal(struct TreeNode* root, int* returnSize){
    int* res = malloc(sizeof(int)*100);
    *returnSize=0;
    inorder(root,res,returnSize);
    return res;
}

//迭代法c
int* inorderTraversal(struct TreeNode* root, int* returnSize) {
    *returnSize = 0;
    int* res = malloc(sizeof(int) * 501);
    struct TreeNode** stk = malloc(sizeof(struct TreeNode*) * 501);
    int top = 0;
    while (root != NULL || top > 0) {
        while (root != NULL) {
            //先后入栈根和根左节点的所有的根,左节点
            stk[top++] = root;
            root = root->left;
        }
        //出左节点
        root = stk[--top];
        //赋值左节点和根节点
        res[(*returnSize)++] = root->val;
        //最左边的点没有左节点时会找右节点,没找到root=NULL
        root = root->right;
    }
    return res;
}
//后序遍历
//递归
void inorder(TreeNode* root,int* res,int*returnSize){
	if(root==NULL)
		return;
	inorder(root->left,res,returnSize);
	inorder(root->right,res,returnSize);
	res[(*returnSize)++] = root->val;
}
void inorderTraversal(TreeNode* root,int*returnSize){
	int* res = malloc(sizeof(int)*200);
	*returnSize=0;
	inorder(root,res,returnSize);
	return res;
}
//迭代法c++
class Solution {
public:
     vector<int> preorderTraversal(TreeNode* root) {
		TreeNode* prev = nullptr;
        vector<int> res;
        if (root == nulltr) {
            return res;
        }
        stack<TreeNode*> stk;
        TreeNode* node = root;
        while (!stk.empty() || node != nullptr) {
            while (node != nullptr) {
                stk.emplace(node);
                node = node->left;
            }
            //第一次最左节点,
            node = stk.top();
            stk.pop();
			if(node->right==nullptr||node->right==prev){
				res.emplace_back(node->val);
				prev=node;
				node=nullptr;
			}else{
                //再次入根节点
				stk.emplace(node);
				node=node->right;
        }
    }
	 return res;
}
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值