LeetCode练习题94、144、590、589

94. 二叉树的中序遍历

给定一个二叉树,返回它的中序 遍历。
示例:
输入: [1,null,2,3]

  1
    \
     2
    /
   3

输出: [1,3,2]
二叉树的中序遍历

递归

#define MAXSIZE 1000
void inorder(struct TreeNode *root, int *returnSize, int *a) {
    if(!root) {return ;}
    inorder(root->left, returnSize, a);
    a[(*returnSize)++] = root->val;
    inorder(root->right, returnSize, a);
}   //中序遍历
int* inorderTraversal(struct TreeNode* root, int* returnSize){
    (*returnSize) = 0;
    int *ret = (int *)malloc(sizeof(int)*MAXSIZE);
    inorder(root, returnSize, ret);
    return ret;
}

144. 二叉树的前序遍历

给定一个二叉树,返回它的 前序 遍历。
示例:
输入: [1,null,2,3]

  1
    \
     2
    /
   3

输出: [1,2,3]
二叉树的前序遍历

迭代

先输出根结点
由于栈先进后出的性质,先入右,再入左

# define MaxSize 1000
int* preorderTraversal(struct TreeNode* root, int* returnSize){
    int *ret = (int *)malloc(sizeof(int)*MaxSize);  //输出数组
    int top = -1;
    (*returnSize) = 0;
    struct TreeNode **stack = (struct TreeNode**)malloc(sizeof(struct TreeNode*)*MaxSize);    //创建存储树结点的栈
    if(root == NULL) {return NULL;}
    stack[++top] = root;
    while(top != -1) {
        root = stack[top--];
        ret[(*returnSize)++] = root->val;
        if(root->right) {
            stack[++top] = root->right;
        }   //右结点入栈
        if(root->left) {
            stack[++top] = root->left;
        }   //左结点入栈
    }
    return ret;
}

590. N叉树的后序遍历

给定一个 N 叉树,返回其节点值的后序遍历。
例如,给定一个 3叉树 :

             1
          /  |  \
        3   2   4
       / \
     5    6

返回其后序遍历: [5,6,3,2,4,1].
N叉树的后序遍历
递归

#define MaxSize 10000
void porder(struct Node *root, int *returnSize, int *a) {
    if(root == NULL) {return NULL;}
    for(int i = 0; i < root->numChildren; i++) {
        porder(root->children[i], returnSize, a);
        a[(*returnSize)++] = root->children[i]->val;
    }
}
int* postorder(struct Node* root, int* returnSize) {
    int *res = (int *)malloc(sizeof(int)*MaxSize);
    (*returnSize) = 0;
    if(root == NULL) {return NULL;}
    porder(root, returnSize, res);
    res[(*returnSize)++] = root->val;
    return res;
}

589. N叉树的前序遍历

给定一个 N 叉树,返回其节点值的前序遍历。
例如,给定一个 3叉树 :

             1
          /  |  \
        3   2   4
       / \
     5    6

返回其前序遍历: [1,3,5,6,2,4]。
N叉树的前序遍历
递归

#define MaxSize 10000
void porder(struct Node *root, int *returnSize, int *a) {
    if(root == NULL) {return NULL;}
    for(int i; i < root->numChildren; i++) {
        a[(*returnSize)++] = root->children[i]->val;
        porder(root->children[i], returnSize, a);
    }
}
int* preorder(struct Node* root, int* returnSize) {
    int *res = (int *)malloc(sizeof(int)*MaxSize);
    (*returnSize) = 0;
    if(root == NULL) {return NULL;}
    res[(*returnSize)++] = root->val;
    porder(root, returnSize, res);
    return res;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值