📌  相关文章
📜  从根到完整二叉树中所有节点的打印路径

📅  最后修改于: 2021-04-29 11:40:35             🧑  作者: Mango

给定数字N ,它是一个完整的二叉树中节点的总数,其中节点的级别是从1到N依次编号。任务是编写一个程序,以打印从根到“完整二进制树”中所有节点的路径。

对于N = 3,树将是:

1
  /     \
2        3 

对于N = 7,树将为:

1
    /     \
   2        3 
 /   \    /  \ 
4    5    6   7

例子:

Input : 7  
Output : 
1 
1 2 
1 2 4 
1 2 5 
1 3 
1 3 6 
1 3 7 

Input : 4 
Output :
1 
1 2 
1 2 4 
1 3 

说明:-因为,给定的树是一个完整的二叉树。对于每个节点i我们可以计算出它的左孩子为2 * i ,右孩子为2 * i +1

这个想法是使用回溯方法来打印所有路径。维护一个向量来存储路径,首先将根节点1推入其中,然后在推动左子节点和右子节点之前,先打印存储在其中的当前路径,然后再为左子节点和右子节点调用该函数。

以下是上述方法的完整实现:

C++
// C++ program to print path from root to all
// nodes in a complete binary tree.
  
#include 
#include 
using namespace std;
  
// Function to print path of all the nodes
// nth node represent as given node
// kth node represents as left and right node
void printPath(vector res, int nThNode, int kThNode)
{
    // base condition
    // if kth node value is greater
    // then nth node then its means
    // kth node is not valid so
    // we not store it into the res
    // simply we just return
    if (kThNode > nThNode)
        return;
  
    // Storing node into res
    res.push_back(kThNode);
  
    // Print the path from root to node
    for (int i = 0; i < res.size(); i++)
        cout << res[i] << " ";
    cout << "\n";
  
    // store left path of a tree
    // So for left we will go node(kThNode*2)
    printPath(res, nThNode, kThNode * 2);
  
    // right path of a tree
    // and for right we will go node(kThNode*2+1)
    printPath(res, nThNode, kThNode * 2 + 1);
}
  
// Function to print path from root to all of the nodes
void printPathToCoverAllNodeUtil(int nThNode)
{
    // res is for store the path
    // from root to particulate node
    vector res;
  
    // Print path from root to all node.
    // third argument 1 because of we have
    // to consider root node is 1
    printPath(res, nThNode, 1);
}
  
// Driver Code
int main()
{
    // Given Node
    int nThNode = 7;
  
    // Print path from root to all node.
    printPathToCoverAllNodeUtil(nThNode);
  
    return 0;
}


Java
// Java program to print path from root to all 
// nodes in a complete binary tree. 
import java.util.*;
  
class GFG
{
  
// Function to print path of all the nodes 
// nth node represent as given node 
// kth node represents as left and right node 
static void printPath(Vector res,
                    int nThNode, int kThNode) 
{ 
    // base condition 
    // if kth node value is greater 
    // then nth node then its means 
    // kth node is not valid so 
    // we not store it into the res 
    // simply we just return 
    if (kThNode > nThNode) 
        return; 
  
    // Storing node into res 
    res.add(kThNode); 
  
    // Print the path from root to node 
    for (int i = 0; i < res.size(); i++) 
        System.out.print( res.get(i) + " "); 
    System.out.print( "\n"); 
  
    // store left path of a tree 
    // So for left we will go node(kThNode*2) 
    printPath(res, nThNode, kThNode * 2); 
  
    // right path of a tree 
    // and for right we will go node(kThNode*2+1) 
    printPath(res, nThNode, kThNode * 2 + 1); 
      
    res.remove(res.size()-1);
} 
  
// Function to print path from root to all of the nodes 
static void printPathToCoverAllNodeUtil(int nThNode) 
{ 
    // res is for store the path 
    // from root to particulate node 
    Vector res=new Vector(); 
  
    // Print path from root to all node. 
    // third argument 1 because of we have 
    // to consider root node is 1 
    printPath(res, nThNode, 1); 
} 
  
// Driver Code 
public static void main(String args[])
{ 
    // Given Node 
    int nThNode = 7; 
  
    // Print path from root to all node. 
    printPathToCoverAllNodeUtil(nThNode); 
}
}
  
// This code is contributed by Arnab Kundu


Python3
# Python3 program to print path from root 
# to all nodes in a complete binary tree. 
  
# Function to print path of all the nodes 
# nth node represent as given node kth
# node represents as left and right node 
def printPath(res, nThNode, kThNode): 
  
    # base condition 
    # if kth node value is greater 
    # then nth node then its means 
    # kth node is not valid so 
    # we not store it into the res 
    # simply we just return 
    if kThNode > nThNode: 
        return
  
    # Storing node into res 
    res.append(kThNode) 
  
    # Print the path from root to node 
    for i in range(0, len(res)): 
        print(res[i], end = " ") 
    print() 
  
    # store left path of a tree 
    # So for left we will go node(kThNode*2) 
    printPath(res[:], nThNode, kThNode * 2) 
  
    # right path of a tree 
    # and for right we will go node(kThNode*2+1) 
    printPath(res[:], nThNode, kThNode * 2 + 1) 
  
# Function to print path from root 
# to all of the nodes 
def printPathToCoverAllNodeUtil(nThNode): 
  
    # res is for store the path 
    # from root to particulate node 
    res = []
  
    # Print path from root to all node. 
    # third argument 1 because of we have 
    # to consider root node is 1 
    printPath(res, nThNode, 1) 
  
# Driver Code 
if __name__ == "__main__": 
  
    # Given Node 
    nThNode = 7
  
    # Print path from root to all node. 
    printPathToCoverAllNodeUtil(nThNode) 
  
# This code is contributed by Rituraj Jain


C#
// C# program to print path from root to all 
// nodes in a complete binary tree. 
using System;
using System.Collections.Generic; 
  
class GFG
{
  
// Function to print path of all the nodes 
// nth node represent as given node 
// kth node represents as left and right node 
static void printPath(List res,
                    int nThNode, int kThNode) 
{ 
    // base condition 
    // if kth node value is greater 
    // then nth node then its means 
    // kth node is not valid so 
    // we not store it into the res 
    // simply we just return 
    if (kThNode > nThNode) 
        return; 
  
    // Storing node into res 
    res.Add(kThNode); 
  
    // Print the path from root to node 
    for (int i = 0; i < res.Count; i++) 
        Console.Write( res[i] + " "); 
    Console.Write( "\n"); 
  
    // store left path of a tree 
    // So for left we will go node(kThNode*2) 
    printPath(res, nThNode, kThNode * 2); 
  
    // right path of a tree 
    // and for right we will go node(kThNode*2+1) 
    printPath(res, nThNode, kThNode * 2 + 1); 
      
    res.RemoveAt(res.Count-1);
} 
  
// Function to print path from root to all of the nodes 
static void printPathToCoverAllNodeUtil(int nThNode) 
{ 
    // res is for store the path 
    // from root to particulate node 
    List res=new List(); 
  
    // Print path from root to all node. 
    // third argument 1 because of we have 
    // to consider root node is 1 
    printPath(res, nThNode, 1); 
} 
  
// Driver Code 
public static void Main(String []args)
{ 
    // Given Node 
    int nThNode = 7; 
  
    // Print path from root to all node. 
    printPathToCoverAllNodeUtil(nThNode); 
}
}
  
// This code contributed by Rajput-Ji


PHP
 $nThNode) 
        return; 
  
    // Storing node into res 
    array_push($res, $kThNode); 
  
    // Print the path from root to node 
    for ($i = 0; $i < count($res); $i++) 
        echo $res[$i] . " "; 
    echo "\n"; 
  
    // store left path of a tree 
    // So for left we will go node(kThNode*2) 
    printPath($res, $nThNode, $kThNode * 2); 
  
    // right path of a tree 
    // and for right we will go node(kThNode*2+1) 
    printPath($res, $nThNode, $kThNode * 2 + 1); 
} 
  
// Function to print path 
// from root to all of the nodes 
function printPathToCoverAllNodeUtil($nThNode) 
{ 
    // res is for store the path 
    // from root to particulate node 
    $res = array(); 
  
    // Print path from root to all node. 
    // third argument 1 because of we have 
    // to consider root node is 1 
    printPath($res, $nThNode, 1); 
} 
  
// Driver Code 
  
// Given Node 
$nThNode = 7; 
  
// Print path from root to all node. 
printPathToCoverAllNodeUtil($nThNode); 
  
// This code is contributed by mits
?>


输出:
1 
1 2 
1 2 4 
1 2 5 
1 3 
1 3 6 
1 3 7