📌  相关文章
📜  从根节点到N元树中给定节点的路径

📅  最后修改于: 2021-05-04 12:10:32             🧑  作者: Mango

给定整数N以下形式的N -ary树:

  • 从1开始直到包含节点N的最后一级,每个节点都按顺序编号。
  • 每个奇数级的节点包含2个子节点,每个偶数级的节点包含4个子节点。

任务是打印从根节点到节点N的路径。

例子:

方法:请按照以下步骤解决问题:

  • 初始化一个数组,以存储在树的每个级别中存在的节点数,即{1、2、8、16、64、128…。}并存储它。
  • 计算数组的前缀和,即{1 3 11 27 91 219 …….}
  • 使用lower_bound()在前缀总和数组中找到索引ind ,该索引ind大于或等于N。因此, ind指示到达节点N所需遍历的级别数。
  • 初始化一个变量temp = N和一个数组path []来存储从根到N的节点。
  • 递减ind直到它小于或等于1,并继续更新val = temp – prefix [ind – 1]
  • 如果ind为奇数,则更新temp = prefix [ind – 2] +(val +1)/ 2。
  • 否则,如果ind为偶数,则更新temp = prefix [ind – 2] +(val + 3)/ 4。
  • temp附加到path []数组中。
  • 最后,打印数组path []

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
typedef long long ll;
 
// Function to find the path
// from root to N
void PrintPathNodes(ll N)
{
 
    // Stores the number of
    // nodes at (i + 1)-th level
    vector arr;
    arr.push_back(1);
 
    // Stores the number of nodes
    ll k = 1;
 
    // Stores if the current
    // level is even or odd
    bool flag = true;
 
    while (k < N) {
 
        // If level is odd
        if (flag == true) {
            k *= 2;
            flag = false;
        }
 
        // If level is even
        else {
 
            k *= 4;
            flag = true;
        }
 
        // If level with
        // node N is reached
        if (k > N) {
            break;
        }
 
        // Push into vector
        arr.push_back(k);
    }
 
    ll len = arr.size();
    vector prefix(len);
    prefix[0] = 1;
 
    // Compute prefix sums of count
    // of nodes in each level
    for (ll i = 1; i < len; ++i) {
        prefix[i] = arr[i] + prefix[i - 1];
    }
 
    vector::iterator it
        = lower_bound(prefix.begin(),
                      prefix.end(), N);
 
    // Stores the level in which
    // node N s present
    ll ind = it - prefix.begin();
 
    ll temp = N;
 
    // Store path
    vector path;
 
    path.push_back(N);
 
    while (ind > 1) {
        ll val = temp - prefix[ind - 1];
 
        if (ind % 2 != 0) {
            temp = prefix[ind - 2]
                   + (val + 1) / 2;
        }
        else {
            temp = prefix[ind - 2]
                   + (val + 3) / 4;
        }
        --ind;
 
        // Insert temp into path
        path.push_back(temp);
    }
 
    if (N != 1)
        path.push_back(1);
 
    // Print path
    for (int i = path.size() - 1;
         i >= 0; i--) {
 
        cout << path[i] << " ";
    }
}
 
// Driver Code
int main()
{
 
    ll N = 14;
 
    // Function Call
    PrintPathNodes(N);
 
    return 0;
}


Python3
# Python3 program for the above approach
from bisect import bisect_left
 
# Function to find the path
# from root to N
def PrintPathNodes(N):
 
    # Stores the number of
    # nodes at (i + 1)-th level
    arr = []
    arr.append(1)
 
    # Stores the number of nodes
    k = 1
 
    # Stores if the current
    # level is even or odd
    flag = True
    while (k < N):
 
        # If level is odd
        if (flag == True):
            k *= 2
            flag = False
 
        # If level is even
        else:
            k *= 4
            flag = True
 
        # If level with
        # node N is reached
        if (k > N):
            break
 
        # Push into vector
        arr.append(k)
    lenn = len(arr)
    prefix = [0]*(lenn)
    prefix[0] = 1
 
    # Compute prefix sums of count
    # of nodes in each level
    for i in range(1,lenn):
        prefix[i] = arr[i] + prefix[i - 1]
    it = bisect_left(prefix, N)
 
    # Stores the level in which
    # node N s present
    ind = it
    temp = N
 
    # Store path
    path = []
    path.append(N)
    while (ind > 1):
        val = temp - prefix[ind - 1]
 
        if (ind % 2 != 0):
            temp = prefix[ind - 2] + (val + 1) // 2
        else:
            temp = prefix[ind - 2] + (val + 3) // 4
        ind -= 1
 
        # Insert temp into path
        path.append(temp)
    if (N != 1):
        path.append(1)
 
    # Print path
    for i in range(len(path)-1, -1, -1):
        print(path[i], end=" ")
 
# Driver Code
if __name__ == '__main__':
    N = 14
 
    # Function Call
    PrintPathNodes(N)
 
    # This code is contributed by mohit kumar 29


输出:
1 2 5 14

时间复杂度: O(log(N))
辅助空间: O(log(N))