📌  相关文章
📜  通过用所有剩余节点的乘积替换每个节点来修改二叉树

📅  最后修改于: 2021-09-04 11:24:12             🧑  作者: Mango

给定一个由N 个节点组成的二叉树,任务是用所有剩余节点的乘积替换树的每个节点。

例子:

方法:可以通过先计算给定树中所有节点的乘积来解决给定问题,然后对给定树执行任意树遍历,并通过值(P/(root->values)更新每个节点。遵循解决问题的步骤如下:

  • 使用 Preorder Traversal 找到二叉树所有节点的乘积并将其存储在变量P 中
  • 定义一个函数,比如updateTree(root, P) ,并执行以下步骤:
    • 如果root 的值为NULL ,则从函数返回。
    • 将根节点的当前值更新为值(P/root->value)
    • 递归调用左右子树作为updateTree(root->left, P)updateTree(root->right, P)
  • 完成上述步骤后,打印修改后的树的中序遍历。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// A Tree node
class Node {
public:
    int data;
    Node *left, *right;
 
    // Constructor
    Node(int data)
    {
        this->data = data;
        left = NULL;
        right = NULL;
    }
};
 
// Function to create a new node in
// the given Tree
Node* newNode(int value)
{
    Node* temp = new Node(value);
    return (temp);
}
 
// Function to find the product of
// all the nodes in the given Tree
int findProduct(Node* root)
{
    // Base Case
    if (root == NULL)
        return 1;
 
    // Recursively Call for the left
    // and the right subtree
    return (root->data
            * findProduct(root->left)
            * findProduct(root->right));
}
 
// Function to perform the Inorder
// traversal of the given tree
void display(Node* root)
{
    // Base Case
    if (root == NULL)
        return;
 
    // Recursively call for the
    // left subtree
    display(root->left);
 
    // Print the value
    cout << root->data << " ";
 
    // Recursively call for the
    // right subtree
    display(root->right);
}
 
// Function to convert the given tree
// to the multiplication tree
void convertTree(int product,
                 Node* root)
{
    // Base Case
    if (root == NULL)
        return;
 
    // Divide the total product by
    // the root's data
    root->data = product / (root->data);
 
    // Go to the left subtree
    convertTree(product, root->left);
 
    // Go to the right subtree
    convertTree(product, root->right);
}
 
// Driver Code
int main()
{
    // Given Binary Tree
    Node* root = newNode(1);
    root->left = newNode(2);
    root->right = newNode(3);
 
    root->right->left = newNode(4);
    root->right->right = newNode(5);
 
    cout << "Inorder Traversal of "
         << "given Tree:\n";
 
    // Print the tree traversal
    display(root);
 
    int product = findProduct(root);
 
    cout << "\nInorder Traversal of "
         << "given Tree:\n";
 
    // Function Call
    convertTree(product, root);
 
    // Print the tree traversal
    display(root);
 
    return 0;
}


Python3
# Python3 program for the above approach
 
# A Tree node
class Node:
     
    def __init__(self, d):
         
        self.data = d
        self.left = None
        self.right = None
 
# Function to find the product of
# all the nodes in the given Tree
def findProduct(root):
     
    # Base Case
    if (root == None):
        return 1
 
    # Recursively Call for the left
    # and the right subtree
    return (root.data * findProduct(root.left) *
                        findProduct(root.right))
 
# Function to perform the Inorder
# traversal of the given tree
def display(root):
     
    # Base Case
    if (root == None):
        return
 
    # Recursively call for the
    # left subtree
    display(root.left)
 
    # Print the value
    print(root.data, end = " ")
 
    # Recursively call for the
    # right subtree
    display(root.right)
 
# Function to convert the given tree
# to the multiplication tree
def convertTree(product, root):
     
    # Base Case
    if (root == None):
        return
 
    # Divide the total product by
    # the root's data
    root.data = product // (root.data)
 
    # Go to the left subtree
    convertTree(product, root.left)
 
    # Go to the right subtree
    convertTree(product, root.right)
 
# Driver Code
if __name__ == '__main__':
     
    # Given Binary Tree
    root = Node(1)
    root.left = Node(2)
    root.right = Node(3)
    root.right.left = Node(4)
    root.right.right = Node(5)
 
    print("Inorder Traversal of given Tree: ")
 
    # Print the tree traversal
    display(root)
 
    product = findProduct(root)
 
    print("\nInorder Traversal of given Tree:")
 
    # Function Call
    convertTree(product, root)
 
    # Print the tree traversal
    display(root)
 
# This code is contributed by mohit kumar 29


Java
// Java program for the above approch
public class GFG {
    // TreeNode class
    static class Node {
        public int data;
        public Node left, right;
    };
 
    static Node newNode(int key)
    {
        Node temp = new Node();
        temp.data = key;
        temp.left = temp.right = null;
        return temp;
    }
 
    // Function to find the product of
    // all the nodes in the given Tree
    static int findProduct(Node root)
    {
        // Base Case
        if (root == null)
            return 1;
 
        // Recursively Call for the left
        // and the right subtree
        return (root.data * findProduct(root.left)
                * findProduct(root.right));
    }
 
    // Function to perform the Inorder
    // traversal of the given tree
    static void display(Node root)
    {
        // Base Case
        if (root == null)
            return;
 
        // Recursively call for the
        // left subtree
        display(root.left);
 
        // Print the value
        System.out.print(root.data + " ");
 
        // Recursively call for the
        // right subtree
        display(root.right);
    }
 
    // Function to convert the given tree
    // to the multiplication tree
    static void convertTree(int product, Node root)
    {
        // Base Case
        if (root == null)
            return;
 
        // Divide the total product by
        // the root's data
        root.data = product / (root.data);
 
        // Go to the left subtree
        convertTree(product, root.left);
 
        // Go to the right subtree
        convertTree(product, root.right);
    }
 
    // Driver code
    public static void main(String[] args)
    {
        // Given Binary Tree
        Node root = newNode(1);
        root.left = newNode(2);
        root.right = newNode(3);
 
        root.right.left = newNode(4);
        root.right.right = newNode(5);
 
        System.out.println("Inorder Traversal of "
                           + "given Tree:");
 
        // Print the tree traversal
        display(root);
 
        int product = findProduct(root);
 
        System.out.println("\nInorder Traversal of "
                           + "given Tree:");
 
        // Function Call
        convertTree(product, root);
 
        // Print the tree traversal
        display(root);
    }
}
// This code is contributed by abhinavjain194


C#
// C# program for the above approch
using System;
 
public class GFG {
    // TreeNode class
    class Node {
        public int data;
        public Node left, right;
    };
 
    static Node newNode(int key)
    {
        Node temp = new Node();
        temp.data = key;
        temp.left = temp.right = null;
        return temp;
    }
 
    // Function to find the product of
    // all the nodes in the given Tree
    static int findProduct(Node root)
    {
        // Base Case
        if (root == null)
            return 1;
 
        // Recursively Call for the left
        // and the right subtree
        return (root.data * findProduct(root.left)
                * findProduct(root.right));
    }
 
    // Function to perform the Inorder
    // traversal of the given tree
    static void display(Node root)
    {
        // Base Case
        if (root == null)
            return;
 
        // Recursively call for the
        // left subtree
        display(root.left);
 
        // Print the value
        Console.Write(root.data + " ");
 
        // Recursively call for the
        // right subtree
        display(root.right);
    }
 
    // Function to convert the given tree
    // to the multiplication tree
    static void convertTree(int product, Node root)
    {
        // Base Case
        if (root == null)
            return;
 
        // Divide the total product by
        // the root's data
        root.data = product / (root.data);
 
        // Go to the left subtree
        convertTree(product, root.left);
 
        // Go to the right subtree
        convertTree(product, root.right);
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        // Given Binary Tree
        Node root = newNode(1);
        root.left = newNode(2);
        root.right = newNode(3);
 
        root.right.left = newNode(4);
        root.right.right = newNode(5);
 
        Console.WriteLine("Inorder Traversal of "
                           + "given Tree:");
 
        // Print the tree traversal
        display(root);
 
        int product = findProduct(root);
 
        Console.WriteLine("\nInorder Traversal of "
                           + "given Tree:");
 
        // Function Call
        convertTree(product, root);
 
        // Print the tree traversal
        display(root);
    }
}
 
// This code is contributed by Amit Katiyar


输出:
Inorder Traversal of given Tree:
2 1 4 3 5 
Inorder Traversal of given Tree:
60 120 30 40 24

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live