📌  相关文章
📜  查找给定二叉树的所有重复级别

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

查找给定二叉树的所有重复级别

给定二叉树,其中所有节点的值都为 01 ,任务是查找并打印存在另一个级别的所有级别,以使每个级别的十进制表示相同。如果不存在这样的级别,则返回一个空列表。

例子:

方法:解决问题的思路是基于以下观察:

请按照以下步骤解决此问题:

  • 做从根到叶的水平顺序遍历
  • 对于每个级别,将其转换为十进制等效值并将值以{decimal_number, level_number}格式存储在映射中。
  • 然后,只需检查十进制键是否有多个级别。
  • 如果是,则将级别打印为副本。
  • 如果不存在这样的级别,则返回一个空列表。

下面是上述方法的实现:

C#
// C# program to implement above approach
 
using System;
using System.Collections.Generic;
using System.Linq;
 
// Class containing left and right
// child of current node and key value
public class Node {
    public int data;
    public Node left, right;
    public Node(int item)
    {
        data = item;
        left = right = null;
    }
}
 
public class LevelInfo {
    public int level;
    public int length;
}
 
public class GFG {
 
    // Root of the Binary Tree
    public Node root;
    Dictionary > duplicateMap
        = new Dictionary >();
 
    public virtual List >
    printDuplicateLevels(Node root)
    {
        int h = height(root);
        int i;
 
        List > dup_levels
            = new List >();
 
        // Initialize for the root level
        var zerolevelInfo
            = new LevelInfo() { level = 0,
                                length = 0 };
 
        duplicateMap[root.data]
            = new List() { zerolevelInfo };
 
        for (i = 1; i <= h; i++) {
            List currentLevel
                = new List();
            var currentLevelOrder
                = getCurrentLevel(root, i,
                                  currentLevel)
                      .ToList();
 
            int decimalValue = Convert.ToInt32(
                string.Join("", currentLevelOrder), 2);
 
            var currentlevelInfo = new LevelInfo() {
                level = i - 1, length
                               = currentLevelOrder.Count()
            };
 
            if (duplicateMap.ContainsKey(decimalValue)) {
                var dictData
                    = duplicateMap[decimalValue].Where(
                        l => l.length
                            == currentLevelOrder.Count());
                if (dictData.Any()) {
                    List dup_level_curr
                        = new List();
                    dup_level_curr.Add(i - 1);
                    dup_level_curr.Add(
                        dictData.Select(l => l.level)
                            .First());
                    dup_levels.Add(dup_level_curr);
                }
                else {
                    duplicateMap[decimalValue].Add(
                        currentlevelInfo);
                }
            }
            else
                duplicateMap[decimalValue]
                    = new List() {
                          currentlevelInfo
                      };
        }
 
        return dup_levels;
    }
 
    // Compute the "height" of a tree
    public virtual int height(Node root)
    {
        if (root == null) {
            return 0;
        }
        else {
 
            // Compute height of each subtree
            int lheight = height(root.left);
            int rheight = height(root.right);
 
            // Use the larger one
            if (lheight > rheight) {
                return (lheight + 1);
            }
            else {
                return (rheight + 1);
            }
        }
    }
 
    // Get nodes at the current level
    public virtual IList
    getCurrentLevel(Node root, int level,
                    List currentLevelOrder)
    {
        if (root == null) {
            return currentLevelOrder;
        }
        if (level == 1) {
            currentLevelOrder.Add(root.data);
        }
        else if (level > 1) {
            getCurrentLevel(root.left, level - 1,
                            currentLevelOrder);
            getCurrentLevel(root.right, level - 1,
                            currentLevelOrder);
        }
        return currentLevelOrder;
    }
 
    // Driver Code
    public static void Main(string[] args)
    {
        GFG tree = new GFG();
        tree.root = new Node(1);
        tree.root.left = new Node(0);
        tree.root.right = new Node(1);
 
        tree.root.left.left = new Node(1);
        tree.root.left.right = new Node(0);
 
        tree.root.right.left = new Node(1);
 
        tree.root.left.right.left = new Node(0);
        tree.root.right.left.right = new Node(1);
 
        tree.root.left.right.left.left = new Node(1);
 
        // Execute and print the duplicate levels
        List > dup_levels
            = tree.printDuplicateLevels(tree.root);
 
        Console.Write("[ ");
        foreach(var curr_level in dup_levels)
        {
            Console.Write("[ ");
            foreach(var dupli_level in curr_level)
            {
                Console.Write(dupli_level + " ");
            }
            Console.Write("] ");
        }
        Console.WriteLine("]");
    }
}


输出
[ [ 3 1 ] [ 4 0 ] ]

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