zoukankan      html  css  js  c++  java
  • 【leetcode】94. 二叉树的中序遍历

    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 = root->right;
        }
        return res;
    }
    void recursion(struct TreeNode* root, int* returnSize,int* arr){
        if(!root) return;
        recursion(root->left,returnSize,arr);
        arr[(*returnSize)++]=root->val;
        recursion(root->right,returnSize,arr);
    }
    int* inorderTraversal(struct TreeNode* root, int* returnSize){
        *returnSize=0;
        int* arr=(int*)calloc(1000,sizeof(int));
        recursion(root,returnSize,arr);
        return arr;
    }
  • 相关阅读:
    String to Integer (atoi)
    Reverse Integer
    ZigZag Conversion
    01-GIT
    04-Eclipse操作SVN
    03-客户端访问SVN服务器
    02-Subversion安装与配置
    01-SVN概述
    09-Spring整合之SSH
    08-Spring的事务管理
  • 原文地址:https://www.cnblogs.com/ganxiang/p/14133474.html
Copyright © 2011-2022 走看看