📌  相关文章
📜  通过将偶数和奇数级别的所有节点分别替换为其最近的偶数或奇数完美平方来修改二叉树

📅  最后修改于: 2021-09-07 02:14:11             🧑  作者: Mango

给定由N 个节点组成的二叉树,任务是将二叉树中出现在偶数层的所有节点替换为其最近的偶完全平方,并用最近的奇完全平方替换奇数层的节点。

例子:

方法:给定的问题可以使用 Level Order Traversal 解决。请按照以下步骤解决问题:

  1. 初始化一个队列,比如说q。
  2. 推入队列。
  3. 队列不为空时循环
    • 将当前节点存储在一个变量中,比如temp_node
    • 如果当前节点值是一个完美的正方形,请检查以下内容:
      • 如果 level 的值为奇数,并且当前节点的值为偶数,则找到最近的奇完全平方并更新temp_node→data
      • 如果 level 的值为偶数且当前节点的值为奇数,则找到最近的偶完全平方并更新temp_node→data
    • 否则,如果 level 的值为奇数,则找到最接近的奇完全平方;如果 level 的值为偶数,则找到最接近的偶完全平方。更新temp_node→data
    • 打印temp_node→data
    • 如果存在,将temp_node的孩子(首先是孩子,然后是孩子)排队q
    • q出列当前节点。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Structure of a
// Binary Tree Node
struct Node {
    int data;
    struct Node *left, *right;
};
 
// Function to replace all nodes
// at even and odd levels with their
// nearest even or odd perfect squares
void LevelOrderTraversal(Node* root)
{
    // Base Case
    if (root == NULL)
        return;
 
    // Create an empty queue
    // for level order traversal
    queue q;
 
    // Enqueue root
    q.push(root);
 
    // Initialize height
    int lvl = 1;
 
    // Iterate until queue is not empty
    while (q.empty() == false) {
 
        // Store the size
        // of the queue
        int n = q.size();
 
        // Traverse in range [1, n]
        for (int i = 0; i < n; i++) {
 
            // Store the current node
            Node* node = q.front();
 
            // Store its square root
            double num = sqrt(node->data);
            int x1 = floor(num);
            int x2 = ceil(num);
 
            // Check if it is a perfect square
            if (x1 == x2) {
 
                // If level is odd and value is even,
                // find the closest odd perfect square
                if ((lvl & 1) && !(x1 & 1)) {
 
                    int num1 = x1 - 1, num2 = x1 + 1;
 
                    node->data
                        = (abs(node->data - num1 * num1)
                           < abs(node->data - num2 * num2))
                              ? (num1 * num1)
                              : (num2 * num2);
                }
 
                // If level is even and value is odd,
                // find the closest even perfect square
                if (!(lvl & 1) && (x1 & 1)) {
 
                    int num1 = x1 - 1, num2 = x1 + 1;
 
                    node->data
                        = (abs(node->data - num1 * num1)
                           < abs(node->data - num2 * num2))
                              ? (num1 * num1)
                              : (num2 * num2);
                }
            }
 
            // Otherwise, find the find
            // the nearest perfect square
            else {
                if (lvl & 1)
                    node->data
                        = (x1 & 1) ? (x1 * x1) : (x2 * x2);
                else
                    node->data
                        = (x1 & 1) ? (x2 * x2) : (x1 * x1);
            }
 
            // Print front of queue
            // and remove it from queue
            cout << node->data << " ";
            q.pop();
 
            // Enqueue left child
            if (node->left != NULL)
                q.push(node->left);
 
            // Enqueue right child
            if (node->right != NULL)
                q.push(node->right);
        }
 
        // Increment the level by 1
        lvl++;
        cout << endl;
    }
}
 
// Utility function to
// create a new tree node
Node* newNode(int data)
{
    Node* temp = new Node;
    temp->data = data;
    temp->left = temp->right = NULL;
    return temp;
}
 
// Driver Code
int main()
{
    // Binary Tree
    Node* root = newNode(5);
    root->left = newNode(3);
    root->right = newNode(2);
    root->right->left = newNode(16);
    root->right->right = newNode(19);
 
    LevelOrderTraversal(root);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
 
// Structure of a
// Binary Tree Node
class Node
{
    int data;
    Node left, right;
     
    Node(int data)
    {
        this.data = data;
        left = right = null;
    }
}
 
class GFG{
     
// Function to replace all nodes
// at even and odd levels with their
// nearest even or odd perfect squares
static void LevelOrderTraversal(Node root)
{
     
    // Base Case
    if (root == null)
        return;
     
    // Create an empty queue
    // for level order traversal
    Queue q = new LinkedList<>();
     
    // Enqueue root
    q.add(root);
     
    // Initialize height
    int lvl = 1;
     
    // Iterate until queue is not empty
    while (q.size() != 0)
    {
         
        // Store the size
        // of the queue
        int n = q.size();
  
        // Traverse in range [1, n]
        for(int i = 0; i < n; i++)
        {
             
            // Store the current node
            Node node = q.peek();
  
            // Store its square root
            double num = Math.sqrt(node.data);
            int x1 = (int)Math.floor(num);
            int x2 = (int)Math.ceil(num);
  
            // Check if it is a perfect square
            if (x1 == x2)
            {
                 
                // If level is odd and value is even,
                // find the closest odd perfect square
                if (((lvl & 1) != 0) && !((x1 & 1) != 0))
                {
                    int num1 = x1 - 1, num2 = x1 + 1;
  
                    node.data = (Math.abs(node.data - num1 * num1) <
                                 Math.abs(node.data - num2 * num2)) ?
                                 (num1 * num1) : (num2 * num2);
                }
  
                // If level is even and value is odd,
                // find the closest even perfect square
                if (!((lvl & 1) != 0) && ((x1 & 1) != 0))
                {
                    int num1 = x1 - 1, num2 = x1 + 1;
  
                    node.data = (Math.abs(node.data - num1 * num1) <
                                 Math.abs(node.data - num2 * num2)) ?
                                 (num1 * num1) : (num2 * num2);
                }
            }
  
            // Otherwise, find the find
            // the nearest perfect square
            else
            {
                if ((lvl & 1) != 0)
                    node.data = (x1 & 1) != 0 ?
                                (x1 * x1) : (x2 * x2);
                else
                    node.data = (x1 & 1) != 0 ?
                                (x2 * x2) : (x1 * x1);
            }
  
            // Print front of queue
            // and remove it from queue
            System.out.print(node.data + " ");
            q.poll();
  
            // Enqueue left child
            if (node.left != null)
                q.add(node.left);
  
            // Enqueue right child
            if (node.right != null)
                q.add(node.right);
        }
  
        // Increment the level by 1
        lvl++;
        System.out.println();
    }
}
 
// Driver Code
public static void main (String[] args)
{
     
    // Binary Tree
    Node root = new Node(5);
    root.left = new Node(3);
    root.right = new Node(2);
    root.right.left = new Node(16);
    root.right.right = new Node(19);
 
    LevelOrderTraversal(root);
}
}
 
// This code is contributed by avanitrachhadiya2155


Python3
# Python3 program for the above approach
from collections import deque
from math import sqrt, ceil, floor
 
# Structure of a
# Binary Tree Node
class Node:
    def __init__(self, x):
        self.data = x
        self.left = None
        self.right = None
 
# Function to replace all nodes
# at even and odd levels with their
# nearest even or odd perfect squares
def LevelOrderTraversal(root):
   
    # Base Case
    if (root == None):
        return
 
    # Create an empty queue
    # for level order traversal
    q = deque()
 
    # Enqueue root
    q.append(root)
 
    # Initialize height
    lvl = 1
 
    # Iterate until queue is not empty
    while (len(q) > 0):
 
        # Store the size
        # of the queue
        n = len(q)
 
        # Traverse in range [1, n]
        for i in range(n):
 
            # Store the current node
            node = q.popleft()
 
            # Store its square root
            num = sqrt(node.data)
            x1 = floor(num)
            x2 = ceil(num)
 
            # Check if it is a perfect square
            if (x1 == x2):
 
                # If level is odd and value is even,
                # find the closest odd perfect square
                if ((lvl & 1) and not (x1 & 1)):
                    num1, num2 = x1 - 1, x1 + 1
                    node.data = (num1 * num1) if (abs(node.data - num1 * num1) < abs(node.data - num2 * num2)) else (num2 * num2)
 
                # If level is even and value is odd,
                # find the closest even perfect square
                if (not (lvl & 1) and (x1 & 1)):
                    num1,num2 = x1 - 1, x1 + 1
                    node.data = (num1 * num1) if (abs(node.data - num1 * num1) < abs(node.data - num2 * num2)) else (num2 * num2)
 
            # Otherwise, find the find
            # the nearest perfect square
            else:
                if (lvl & 1):
                    node.data = (x1 * x1) if (x1 & 1) else (x2 * x2)
                else:
                    node.data = (x2 * x2) if (x1 & 1) else (x1 * x1)
 
            # Prfront of queue
            # and remove it from queue
            print(node.data, end = " ")
 
            # Enqueue left child
            if (node.left != None):
                q.append(node.left)
 
            # Enqueue right child
            if (node.right != None):
                q.append(node.right)
 
        # Increment the level by 1
        lvl += 1
        print()
 
# Driver Code
if __name__ == '__main__':
   
    # Binary Tree
    root = Node(5)
    root.left = Node(3)
    root.right = Node(2)
    root.right.left = Node(16)
    root.right.right = Node(19)
 
    LevelOrderTraversal(root)
 
    # This code is contributed by mohit kumar 29.


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
// Structure of a
// Binary Tree Node
public class Node
{
  public int data;
  public Node left, right;
 
  public Node(int data)
  {
    this.data = data;
    left = right = null;
  }
}
 
public class GFG
{
 
  // Function to replace all nodes
  // at even and odd levels with their
  // nearest even or odd perfect squares
  static void LevelOrderTraversal(Node root)
  {
 
    // Base Case
    if (root == null)
      return;
 
    // Create an empty queue
    // for level order traversal
    Queue q = new Queue();
 
    // Enqueue root
    q.Enqueue(root);
 
    // Initialize height
    int lvl = 1;
 
    // Iterate until queue is not empty
    while (q.Count != 0)
    {
 
      // Store the size
      // of the queue
      int n = q.Count;
 
      // Traverse in range [1, n]
      for(int i = 0; i < n; i++)
      {
 
        // Store the current node
        Node node = q.Peek();
 
        // Store its square root
        double num = Math.Sqrt(node.data);
        int x1 = (int)Math.Floor(num);
        int x2 = (int)Math.Ceiling(num);
 
        // Check if it is a perfect square
        if (x1 == x2)
        {
 
          // If level is odd and value is even,
          // find the closest odd perfect square
          if (((lvl & 1) != 0) && !((x1 & 1) != 0))
          {
            int num1 = x1 - 1, num2 = x1 + 1;
 
            node.data = (Math.Abs(node.data - num1 * num1) <
                         Math.Abs(node.data - num2 * num2)) ?
              (num1 * num1) : (num2 * num2);
          }
 
          // If level is even and value is odd,
          // find the closest even perfect square
          if (!((lvl & 1) != 0) && ((x1 & 1) != 0))
          {
            int num1 = x1 - 1, num2 = x1 + 1;
 
            node.data = (Math.Abs(node.data - num1 * num1) <
                         Math.Abs(node.data - num2 * num2)) ?
              (num1 * num1) : (num2 * num2);
          }
        }
 
        // Otherwise, find the find
        // the nearest perfect square
        else
        {
          if ((lvl & 1) != 0)
            node.data = (x1 & 1) != 0 ?
            (x1 * x1) : (x2 * x2);
          else
            node.data = (x1 & 1) != 0 ?
            (x2 * x2) : (x1 * x1);
        }
 
        // Print front of queue
        // and remove it from queue
        Console.Write(node.data + " ");
        q.Dequeue();
 
        // Enqueue left child
        if (node.left != null)
          q.Enqueue(node.left);
 
        // Enqueue right child
        if (node.right != null)
          q.Enqueue(node.right);
      }
 
      // Increment the level by 1
      lvl++;
      Console.WriteLine();
    }
  }
 
  // Driver Code
  static public void Main ()
  {
 
    // Binary Tree
    Node root = new Node(5);
    root.left = new Node(3);
    root.right = new Node(2);
    root.right.left = new Node(16);
    root.right.right = new Node(19);
 
    LevelOrderTraversal(root);
  }
}
 
// This code is contributed by rag2127


输出:
9 
4 4 
9 25

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

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