C++ 递归创建并遍历树

C++ 递归创建并遍历树

**

输入1 2 0 0 3 0 0

**

#include <bits/stdc++.h>
using namespace std;
typedef struct Tree{
    int root;
    Tree *left;
    Tree *right;
}*BiTree;

void dfs(BiTree &tree) {
    int n;
    cin >> n;
    if (n == 0) { tree = NULL; return;}
    tree = new Tree;
    tree->root = n;
    dfs(tree->left);
    dfs(tree->right);

}
// 根左右
void printfTree(BiTree tree) {
    if(tree) {
        cout << tree->root << " ";
        printfTree(tree->left);
        printfTree(tree->right);
    }
}
int main(){
    BiTree tree;
    dfs(tree);
    printfTree(tree);
    system("Pause");
    return 0;
}

例:
在这里插入图片描述

#include <bits/stdc++.h>
using namespace std;
struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
};
class Solution {
public:
    int t[17] = {5,4,1,0,0,11,2,0,0,7,0,0,8,0,9,0,0}; // 输入的结点
    int j = -1;
    void buildTree(TreeNode* &root) {
        j++;
        if (t[j] == 0) {
            root = NULL;
            return;
        }
        root = new TreeNode;
        root->val = t[j];
        
        buildTree(root->left);
        buildTree(root->right);
    }
    int main() {
    Solution s;
    TreeNode *root;
    s.buildTree(root);
    system("Pause");
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
二叉树的非递归算法通常使用栈来实现。具体实现可以参考以下代码: ```c #include <stdio.h> #include <stdlib.h> // 定义的结构体 typedef struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; } TreeNode; // 定义栈的结构体 typedef struct Stack { int top; int capacity; TreeNode **arr; } Stack; // 初始化栈 Stack *initStack(int capacity) { Stack *stack = (Stack *)malloc(sizeof(Stack)); stack->top = -1; stack->capacity = capacity; stack->arr = (TreeNode **)malloc(sizeof(TreeNode *) * capacity); return stack; } // 判断栈是否为空 int isEmpty(Stack *stack) { return stack->top == -1; } // 判断栈是否已满 int isFull(Stack *stack) { return stack->top == stack->capacity - 1; } // 入栈 void push(Stack *stack, TreeNode *node) { if (isFull(stack)) { printf("Stack is full.\n"); return; } stack->arr[++stack->top] = node; } // 出栈 TreeNode *pop(Stack *stack) { if (isEmpty(stack)) { printf("Stack is empty.\n"); return NULL; } return stack->arr[stack->top--]; } // 获取栈顶元素 TreeNode *top(Stack *stack) { if (isEmpty(stack)) { printf("Stack is empty.\n"); return NULL; } return stack->arr[stack->top]; } // 创建新节点 TreeNode *newNode(int val) { TreeNode *node = (TreeNode *)malloc(sizeof(TreeNode)); node->val = val; node->left = NULL; node->right = NULL; return node; } // 构建二叉树 TreeNode *buildTree() { TreeNode *root = newNode(1); root->left = newNode(2); root->right = newNode(3); root->left->left = newNode(4); root->left->right = newNode(5); root->right->left = newNode(6); root->right->right = newNode(7); return root; } // 非递归前序遍历 void preorderTraversal(TreeNode *root) { if (root == NULL) { return; } Stack *stack = initStack(100); push(stack, root); while (!isEmpty(stack)) { TreeNode *node = pop(stack); printf("%d ", node->val); if (node->right != NULL) { push(stack, node->right); } if (node->left != NULL) { push(stack, node->left); } } printf("\n"); } // 非递归中序遍历 void inorderTraversal(TreeNode *root) { if (root == NULL) { return; } Stack *stack = initStack(100); TreeNode *node = root; while (node != NULL || !isEmpty(stack)) { while (node != NULL) { push(stack, node); node = node->left; } node = pop(stack); printf("%d ", node->val); node = node->right; } printf("\n"); } // 非递归后序遍历 void postorderTraversal(TreeNode *root) { if (root == NULL) { return; } Stack *stack1 = initStack(100); Stack *stack2 = initStack(100); push(stack1, root); while (!isEmpty(stack1)) { TreeNode *node = pop(stack1); push(stack2, node); if (node->left != NULL) { push(stack1, node->left); } if (node->right != NULL) { push(stack1, node->right); } } while (!isEmpty(stack2)) { TreeNode *node = pop(stack2); printf("%d ", node->val); } printf("\n"); } int main() { TreeNode *root = buildTree(); printf("Preorder traversal: "); preorderTraversal(root); printf("Inorder traversal: "); inorderTraversal(root); printf("Postorder traversal: "); postorderTraversal(root); return 0; } ``` 这里实现了二叉树的非递归前序、中序和后序遍历算法。其中,非递归中序遍历需要用到两个栈,而非递归后序遍历需要用到两个栈,其中一个栈用于辅助遍历,另一个栈用于存储遍历结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值