don't konw why ...


  • 0
    L

    '''
    /**

    • Definition for a binary tree node.
    • struct TreeNode {
    • int val;
      
    • TreeNode *left;
      
    • TreeNode *right;
      
    • TreeNode(int x) : val(x), left(NULL), right(NULL) {}
      
    • };
      */

    typedef struct BNode {
    TreeNode *p;
    int lno;
    }BNode;

    class Solution {
    public:
    vector<double> averageOfLevels(TreeNode* root) {
    vector<double> result;
    BNode que[1000000];
    double sum;
    int n, max, front, rear, Lno;
    front = rear = 0;
    TreeNode *temp;
    ++rear;
    que[rear].p = root;
    que[rear].lno = 1;
    while(front != rear) {
    ++front;
    temp = que[front].p;
    Lno = que[front].lno;
    if(temp -> left != NULL) {
    ++rear;
    que[rear].p = temp -> left;
    que[rear].lno = Lno + 1;
    }
    if(temp -> right != NULL) {
    ++rear;
    que[rear].p = temp -> right;
    que[rear].lno = Lno + 1;
    }
    }

        for(int i = 1; i <= Lno; ++i) {
            sum = n = 0;
            for(int j = 1; j <= rear; ++j) {
                if(que[j].lno == i) {
                    ++n;
                    sum += que[j].p->val;
                }
            }
            if(n != 0) {
                double tmp = sum / n;
                result.push_back(tmp);
            }
        }
        return result;
    }
    

    };
    '''
    when the input number becomes bigger, this code does not work, I don't know why, can anyone tell me? Forgive my poor English...


  • 0
    L

    @luoji said in don't konw why ...:

    '''

    typedef struct BNode {
    TreeNode *p;
    int lno;
    }BNode;

    class Solution {
    public:
    vector<double> averageOfLevels(TreeNode* root) {
    vector<double> result;
    BNode que[1000000];
    double sum;
    int n, max, front, rear, Lno;
    front = rear = 0;
    TreeNode *temp;
    ++rear;
    que[rear].p = root;
    que[rear].lno = 1;
    while(front != rear) {
    ++front;
    temp = que[front].p;
    Lno = que[front].lno;
    if(temp -> left != NULL) {
    ++rear;
    que[rear].p = temp -> left;
    que[rear].lno = Lno + 1;
    }
    if(temp -> right != NULL) {
    ++rear;
    que[rear].p = temp -> right;
    que[rear].lno = Lno + 1;
    }
    }

        for(int i = 1; i <= Lno; ++i) {
            sum = n = 0;
            for(int j = 1; j <= rear; ++j) {
                if(que[j].lno == i) {
                    ++n;
                    sum += que[j].p->val;
                }
            }
            if(n != 0) {
                double tmp = sum / n;
                result.push_back(tmp);
            }
        }
        return result;
    }
    

    };
    '''
    when the input number becomes bigger, this code does not work, I don't know why, can anyone tell me? Forgive my poor English...

    • >! list item

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.