首页 > 解决方案 > PLSQL - 实现函数,返回树中节点的子节点数

问题描述

我想在 PLSQL 中实现以下功能:https://www.geeksforgeeks.org/number-children-given-node-n-ary-tree/

有没有办法将 queue.push 用作函数?似乎这是不可能的。

// Function to calculate number 
// of children of given node 
int numberOfChildren(Node* root, int x) 
{ 
    // initialize the numChildren as 0 
    int numChildren = 0; 

    if (root == NULL) 
        return 0; 

    // Creating a queue and pushing the root 
    queue<Node*> q; 
    q.push(root); 

    while (!q.empty()) { 
        int n = q.size(); 

        // If this node has children 
        while (n > 0) { 

            // Dequeue an item from queue and 
            // check if it is equal to x 
            // If YES, then return number of children 
            Node* p = q.front(); 
            q.pop(); 
            if (p->key == x) { 
                numChildren = numChildren + p->child.size(); 
                return numChildren; 
            } 

            // Enqueue all children of the dequeued item 
            for (int i = 0; i < p->child.size(); i++) 
                q.push(p->child[i]); 
            n--; 
        } 
    } 
    return numChildren; 
} 

标签: plsqlqueue

解决方案


推荐阅读