📜  通过添加最小节点数将给定的二叉树转换为对称树

📅  最后修改于: 2022-05-13 01:56:05.629000             🧑  作者: Mango

通过添加最小节点数将给定的二叉树转换为对称树

给定二叉树,任务是通过在给定树中添加最小节点数,将给定二叉树转换为对称树。

例子:

方法:要解决此问题,请按照以下步骤操作:

  1. 创建一个函数buildSymmetricTree ,它将接受两个参数root1root2
  2. 这里, root1root2是位于彼此对称位置的节点。
  3. 所以最初, root1root2都将包含根的值,并且在每个递归调用中:
    • 如果 root1 存在但root2不存在,则创建一个与root1值相同的新节点并将其放置在root2的位置。
    • 如果root2存在但root1不存在,也对root1执行上述步骤。
    • 如果root1root2的值不同,则将两个节点的值更改为它们的和。
    • 现在,对(root1->left, root2->right)(root1->right, root2->left) 处的对称位置进行下两次递归调用。
  4. 在进行所有递归调用后,树将变得对称。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Node
class Node {
public:
    int val;
    Node *left, *right;
    Node(int val)
    {
        this->val = val;
        left = right = NULL;
    }
};
 
// Function to convert the given tree
// into a symmetric
Node* buidSymmetericTree(Node* root1,
                         Node* root2)
{
    // Base Case
    if (root1 == NULL and root2 == NULL) {
        return NULL;
    }
 
    // If root1 == NULL & root2 != NULL
    if (root1 == NULL) {
 
        // Create new node for root2
        // and attaching it to tree
        Node* node = new Node(root2->val);
        root1 = node;
    }
 
    // If root2 == NULL and root1 != NULL
    if (root2 == NULL) {
 
        // Create new node for root1
        // and attaching it to tree
        Node* node = new Node(root1->val);
        root2 = node;
    }
 
    // If both nodes are different
    // then change both nodes values
    // to the sum of them
    if (root1->val != root2->val) {
        int temp = root1->val + root2->val;
        root1->val = temp;
        root2->val = temp;
    }
 
    // Recurring to the left
    root1->left
        = buidSymmetericTree(
            root1->left, root2->right);
 
    // Recurring to the right
    root1->right
        = buidSymmetericTree(
            root1->right, root2->left);
 
    // Return root pointer
    return root1;
}
 
// Function to perform the Inorder
// Traversal of the tree
void inorder(Node* root)
{
    // Base Case
    if (root == NULL)
        return;
 
    inorder(root->left);
    cout << root->val << " ";
    inorder(root->right);
}
 
// Driver Code
int main()
{
    Node* root = new Node(3);
    root->left = new Node(2);
    root->right = new Node(4);
    root->left->left = new Node(5);
 
    // Function to make the given
    // tree symmetric
    buidSymmetericTree(root, root);
 
    // Print the inorder traversal
    inorder(root);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG{
 
// Node
static class Node {
    int val;
    Node left, right;
    Node(int val)
    {
        this.val = val;
        left = right = null;
    }
};
 
// Function to convert the given tree
// into a symmetric
static Node buidSymmetericTree(Node root1,
                         Node root2)
{
    // Base Case
    if (root1 == null && root2 == null) {
        return null;
    }
 
    // If root1 == null & root2 != null
    if (root1 == null) {
 
        // Create new node for root2
        // and attaching it to tree
        Node node = new Node(root2.val);
        root1 = node;
    }
 
    // If root2 == null and root1 != null
    if (root2 == null) {
 
        // Create new node for root1
        // and attaching it to tree
        Node node = new Node(root1.val);
        root2 = node;
    }
 
    // If both nodes are different
    // then change both nodes values
    // to the sum of them
    if (root1.val != root2.val) {
        int temp = root1.val + root2.val;
        root1.val = temp;
        root2.val = temp;
    }
 
    // Recurring to the left
    root1.left
        = buidSymmetericTree(
            root1.left, root2.right);
 
    // Recurring to the right
    root1.right
        = buidSymmetericTree(
            root1.right, root2.left);
 
    // Return root pointer
    return root1;
}
 
// Function to perform the Inorder
// Traversal of the tree
static void inorder(Node root)
{
    // Base Case
    if (root == null)
        return;
 
    inorder(root.left);
    System.out.print(root.val+ " ");
    inorder(root.right);
}
 
// Driver Code
public static void main(String[] args)
{
    Node root = new Node(3);
    root.left = new Node(2);
    root.right = new Node(4);
    root.left.left = new Node(5);
 
    // Function to make the given
    // tree symmetric
    buidSymmetericTree(root, root);
 
    // Print the inorder traversal
    inorder(root);
}
}
 
// This code is contributed by umadevi9616


Python3
# Python Program to implement
# the above approach
 
# Node
class Node:
 
    def __init__(self, val):
        self.val = val
        self.left = self.right = None
     
 
 
# Function to convert the given tree
# into a symmetric
def buidSymmetericTree(root1, root2):
    # Base Case
    if (root1 == None and root2 == None):
        return None
     
 
    # If root1 == null & root2 != null
    if (root1 == None):
 
        # Create new node for root2
        # and attaching it to tree
        node = Node(root2.val)
        root1 = node
     
 
    # If root2 == null and root1 != null
    if (root2 == None):
 
        # Create new node for root1
        # and attaching it to tree
        node = Node(root1.val)
        root2 = node
     
 
    # If both nodes are different
    # then change both nodes values
    # to the sum of them
    if (root1.val != root2.val):
        temp = root1.val + root2.val
        root1.val = temp
        root2.val = temp
     
 
    # Recuring to the left
    root1.left = buidSymmetericTree( root1.left, root2.right)
 
    # Recurring to the right
    root1.right = buidSymmetericTree(root1.right, root2.left)
 
    # Return root pointer
    return root1
 
 
# Function to perform the Inorder
# Traversal of the tree
def inorder(root):
    # Base Case
    if (root == None):
      return
 
    inorder(root.left)
    print(root.val, end=" ")
    inorder(root.right)
 
 
# Driver Code
 
root = Node(3)
root.left = Node(2)
root.right = Node(4)
root.left.left = Node(5)
 
# Function to make the given
# tree symmetric
buidSymmetericTree(root, root)
 
# Print the inorder traversal
inorder(root)
 
# This code is contributed by gfgking.


C#
// C# program for the above approach
using System;
public class GFG {
 
    // Node
public    class Node {
    public    int val;
    public    Node left, right;
 
    public    Node(int val) {
            this.val = val;
            left = right = null;
        }
    };
 
    // Function to convert the given tree
    // into a symmetric
    static Node buidSymmetericTree(Node root1, Node root2)
    {
       
        // Base Case
        if (root1 == null && root2 == null) {
            return null;
        }
 
        // If root1 == null & root2 != null
        if (root1 == null) {
 
            // Create new node for root2
            // and attaching it to tree
            Node node = new Node(root2.val);
            root1 = node;
        }
 
        // If root2 == null and root1 != null
        if (root2 == null) {
 
            // Create new node for root1
            // and attaching it to tree
            Node node = new Node(root1.val);
            root2 = node;
        }
 
        // If both nodes are different
        // then change both nodes values
        // to the sum of them
        if (root1.val != root2.val) {
            int temp = root1.val + root2.val;
            root1.val = temp;
            root2.val = temp;
        }
 
        // Recurring to the left
        root1.left = buidSymmetericTree(root1.left, root2.right);
 
        // Recurring to the right
        root1.right = buidSymmetericTree(root1.right, root2.left);
 
        // Return root pointer
        return root1;
    }
 
    // Function to perform the Inorder
    // Traversal of the tree
    static void inorder(Node root)
    {
       
        // Base Case
        if (root == null)
            return;
 
        inorder(root.left);
        Console.Write(root.val + " ");
        inorder(root.right);
    }
 
    // Driver Code
    public static void Main(String[] args) {
        Node root = new Node(3);
        root.left = new Node(2);
        root.right = new Node(4);
        root.left.left = new Node(5);
 
        // Function to make the given
        // tree symmetric
        buidSymmetericTree(root, root);
 
        // Print the inorder traversal
        inorder(root);
    }
}
 
// This code is contributed by gauravrajput1


Javascript



输出:
5 6 3 6 5

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