📌  相关文章
📜  对二叉树中每一层的所有低于当前层的最小值节点的所有较低层的节点进行计数

📅  最后修改于: 2021-09-06 05:23:27             🧑  作者: Mango

给定一个 二叉树,每个级别的任务是打印所有较低级别的节点总数,这些节点小于或等于该级别存在的每个节点。

例子:

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

  1. 使用 Level Order Traversal 计算每个级别的最小值。
  2. 对树执行后序遍历并检查每个节点在步骤 1 中计算的节点是否大于或等于该节点。如果发现为真,则在该级别将 count 增加 1,前提是该特定级别的节点存在于其下方的级别,其值小于或等于该级别存在的所有节点。
  3. 打印给出该级别节点数的最终数组。

下面是上述方法的实现:

C++
// C++ program of the
// above approach
 
#include 
using namespace std;
 
// Stores the nodes to be deleted
unordered_map mp;
 
// Structure of a Tree node
struct Node {
    int key;
    struct Node *left, *right;
};
 
// Function to create a new node
Node* newNode(int key)
{
    Node* temp = new Node;
    temp->key = key;
    temp->left = temp->right = NULL;
    return (temp);
}
 
// Function to find the min value
// of node for each level
void calculateMin(Node* root,
                  vector& levelMin)
{
    queue qt;
    qt.push(root);
 
    // Count is used to diffentiate
    // each level of the tree
    int count = 1;
    int min_v = INT_MAX;
    while (!qt.empty()) {
 
        Node* temp = qt.front();
 
        min_v = min(min_v, temp->key);
 
        qt.pop();
 
        if (temp->left) {
            qt.push(temp->left);
        }
        if (temp->right) {
            qt.push(temp->right);
        }
        count--;
        if (count == 0) {
            levelMin.push_back(min_v);
            min_v = INT_MAX;
            count = qt.size();
        }
    }
}
 
// Function to check whether the nodes in
// the level below it are smaller
// by performing post order traversal
void findNodes(Node* root, vector& levelMin,
               vector& levelResult, int level)
{
    if (root == NULL)
        return;
 
    // Traverse the left subtree
    findNodes(root->left, levelMin,
              levelResult, level + 1);
 
    // Traverse right subtree
    findNodes(root->right, levelMin,
              levelResult, level + 1);
 
    // Check from minimum values
    // computed at each level
    for (int i = 0; i < level; i++) {
        if (root->key <= levelMin[i]) {
            levelResult[i] += 1;
        }
    }
}
 
// Function to print count of
// nodes from all lower levels
// having values less than the
// the nodes in the current level
void printNodes(Node* root)
{
    vector levelMin;
    calculateMin(root, levelMin);
 
    // Stores the number of levels
    int numLevels = levelMin.size();
 
    // Stores the required count
    // of nodes for each level
    vector levelResult(numLevels, 0);
    findNodes(root, levelMin, levelResult, 0);
 
    for (int i = 0; i < numLevels; i++) {
        cout << levelResult[i] << " ";
    }
}
 
// Driver Code
int main()
{
    /*
              4
           /     \
           3        5
         /  \     /   \
       10    2   3     1
      */
 
    Node* root = newNode(4);
 
    root->left = newNode(3);
    root->right = newNode(5);
 
    root->right->left = newNode(3);
    root->right->right = newNode(1);
 
    root->left->left = newNode(10);
    root->left->right = newNode(2);
    printNodes(root);
}


Java
// Java program of the
// above approach
import java.util.*;
class GFG{
 
// Stores the nodes to be deleted
static Map mp = new HashMap();
 
  // Structure of a Tree node
static class Node
{
  int key;
  Node left, right;
};
 
// Function to create a new node
static Node newNode(int key)
{
  Node temp = new Node();
  temp.key = key;
  temp.left = temp.right = null;
  return (temp);
}
 
// Function to find the min value
// of node for each level
static void calculateMin(Node root,
                         Vector levelMin)
{
  Queue qt = new LinkedList<>();
  qt.add(root);
 
  // Count is used to diffentiate
  // each level of the tree
  int count = 1;
  int min_v = Integer.MAX_VALUE;
  while (!qt.isEmpty())
  {
    Node temp = qt.peek();
    min_v = Math.min(min_v, temp.key);
    qt.remove();
 
    if (temp.left != null)
    {
      qt.add(temp.left);
    }
    if (temp.right != null)
    {
      qt.add(temp.right);
    }
    count--;
    if (count == 0)
    {
      levelMin.add(min_v);
      min_v = Integer.MAX_VALUE;
      count = qt.size();
    }
  }
}
 
// Function to check whether the nodes in
// the level below it are smaller
// by performing post order traversal
static void findNodes(Node root,
                      Vector levelMin,
                      int []levelResult, int level)
{
  if (root == null)
    return;
 
  // Traverse the left subtree
  findNodes(root.left, levelMin,
            levelResult, level + 1);
 
  // Traverse right subtree
  findNodes(root.right, levelMin,
            levelResult, level + 1);
 
  // Check from minimum values
  // computed at each level
  for (int i = 0; i < level; i++)
  {
    if (root.key <= levelMin.get(i))
    {
      levelResult[i] += 1;
    }
  }
}
 
// Function to print count of
// nodes from all lower levels
// having values less than the
// the nodes in the current level
static void printNodes(Node root)
{
  Vector levelMin =
         new Vector();
  calculateMin(root, levelMin);
 
  // Stores the number of levels
  int numLevels = levelMin.size();
 
  // Stores the required count
  // of nodes for each level
  int []levelResult = new int[numLevels];
  findNodes(root, levelMin, levelResult, 0);
 
  for (int i = 0; i < numLevels; i++)
  {
    System.out.print(levelResult[i] + " ");
  }
}
 
// Driver Code
public static void main(String[] args)
{
  /*
              4
           /     \
           3        5
         /  \     /   \
       10    2   3     1
      */
 
  Node root = newNode(4);
 
  root.left = newNode(3);
  root.right = newNode(5);
 
  root.right.left = newNode(3);
  root.right.right = newNode(1);
 
  root.left.left = newNode(10);
  root.left.right = newNode(2);
  printNodes(root);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program of the
# above approach
from collections import deque
from sys import maxsize as INT_MAX
 
# Stores the nodes to be deleted
mp = dict()
 
# Structure of a Tree node
class Node:
     
    def __init__(self, key):
         
        self.key = key
        self.left = None
        self.right = None
 
# Function to find the min value
# of node for each level
def calculateMin(root: Node,
             levelMin: list) -> None:
 
    qt = deque()
    qt.append(root)
 
    # Count is used to diffentiate
    # each level of the tree
    count = 1
    min_v = INT_MAX
     
    while (qt):
        temp = qt.popleft()
        min_v = min(min_v, temp.key)
 
        if (temp.left):
            qt.append(temp.left)
 
        if (temp.right):
            qt.append(temp.right)
 
        count -= 1
         
        if (count == 0):
            levelMin.append(min_v)
            min_v = INT_MAX
            count = len(qt)
 
# Function to check whether the nodes in
# the level below it are smaller
# by performing post order traversal
def findNodes(root: Node,
          levelMin: list,
       levelResult: list,
             level: int) -> None:
 
    if (root == None):
        return
 
    # Traverse the left subtree
    findNodes(root.left, levelMin,
              levelResult, level + 1)
 
    # Traverse right subtree
    findNodes(root.right, levelMin,
              levelResult, level + 1)
 
    # Check from minimum values
    # computed at each level
    for i in range(level):
        if (root.key <= levelMin[i]):
            levelResult[i] += 1
 
# Function to print count of
# nodes from all lower levels
# having values less than the
# the nodes in the current level
def printNodes(root: Node) -> None:
 
    levelMin = []
    calculateMin(root, levelMin)
 
    # Stores the number of levels
    numLevels = len(levelMin)
 
    # Stores the required count
    # of nodes for each level
    levelResult = [0] * numLevels
    findNodes(root, levelMin, levelResult, 0)
 
    for i in range(numLevels):
        print(levelResult[i], end = " ")
 
# Driver Code
if __name__ == "__main__":
     
    '''
              4
           /     \
          3        5
        /  \     /   \
      10    2   3     1
      '''
 
    root = Node(4)
 
    root.left = Node(3)
    root.right = Node(5)
 
    root.right.left = Node(3)
    root.right.right = Node(1)
 
    root.left.left = Node(10)
    root.left.right = Node(2)
     
    printNodes(root)
 
# This code is contributed by sanjeev2552


C#
// C# program of the
// above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Stores the nodes to be deleted
static Dictionary mp = new Dictionary();
 
// Structure of a Tree node
class Node
{
    public int key;
    public Node left, right;
};
 
// Function to create a new node
static Node newNode(int key)
{
    Node temp = new Node();
    temp.key = key;
    temp.left = temp.right = null;
    return (temp);
}
 
// Function to find the min value
// of node for each level
static void calculateMin(Node root,
                         List levelMin)
{
    Queue qt = new Queue();
    qt.Enqueue(root);
     
    // Count is used to diffentiate
    // each level of the tree
    int count = 1;
    int min_v = int.MaxValue;
     
    while (qt.Count != 0)
    {
        Node temp = qt.Peek();
        min_v = Math.Min(min_v, temp.key);
        qt.Dequeue();
     
        if (temp.left != null)
        {
            qt.Enqueue(temp.left);
        }
        if (temp.right != null)
        {
            qt.Enqueue(temp.right);
        }
        count--;
         
        if (count == 0)
        {
            levelMin.Add(min_v);
            min_v = int.MaxValue;
            count = qt.Count;
        }
    }
}
 
// Function to check whether the nodes in
// the level below it are smaller
// by performing post order traversal
static void findNodes(Node root,
                      List levelMin,
                      int []levelResult,
                      int level)
{
    if (root == null)
        return;
     
    // Traverse the left subtree
    findNodes(root.left, levelMin,
              levelResult, level + 1);
     
    // Traverse right subtree
    findNodes(root.right, levelMin,
              levelResult, level + 1);
     
    // Check from minimum values
    // computed at each level
    for(int i = 0; i < level; i++)
    {
        if (root.key <= levelMin[i])
        {
            levelResult[i] += 1;
        }
    }
}
 
// Function to print count of
// nodes from all lower levels
// having values less than the
// the nodes in the current level
static void printNodes(Node root)
{
    List levelMin = new List();
     
    calculateMin(root, levelMin);
     
    // Stores the number of levels
    int numLevels = levelMin.Count;
     
    // Stores the required count
    // of nodes for each level
    int []levelResult = new int[numLevels];
    findNodes(root, levelMin, levelResult, 0);
     
    for(int i = 0; i < numLevels; i++)
    {
        Console.Write(levelResult[i] + " ");
    }
}
 
// Driver Code
public static void Main(String[] args)
{
/*
           4
         /     \
        3      5
       / \     / \
     10   2 3    1
    */
 
    Node root = newNode(4);
     
    root.left = newNode(3);
    root.right = newNode(5);
     
    root.right.left = newNode(3);
    root.right.right = newNode(1);
     
    root.left.left = newNode(10);
    root.left.right = newNode(2);
    printNodes(root);
}
}
 
// This code is contributed by Amit Katiyar


输出:
4 3 0





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

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