📌  相关文章
📜  检查二叉树的逐级十进制等价物是否形成单调序列

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

检查二叉树的逐级十进制等价物是否形成单调序列

给定二叉树,其中所有节点的值都为01 ,任务是检查给定 Tree 的逐级十进制等值是否形成单调序列。

例子

方法:

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

  • 做从根到叶的水平顺序遍历
  • 对于每个级别,将其转换为等效的十进制并将值存储在整数数组中
  • 然后检查给定的数组是单调递增还是递减

下面是上述方法的实现:

C#
// C# code to check if the tree is monotonic
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 GFG {
    // Root of the Binary Tree
    public Node root;
  
    public virtual bool checkMonotonic(Node root)
    {
        int[] sequenceArray = getLevelOrderArray(root);
        bool increasing = true;
        bool decreasing = true;
        for (int i = 0; i < sequenceArray.Length - 1; ++i) {
            if (sequenceArray[i] > sequenceArray[i + 1])
                increasing = false;
            if (sequenceArray[i] < sequenceArray[i + 1])
                decreasing = false;
        }
        return increasing || decreasing;
    }
  
    public virtual int[] getLevelOrderArray(Node root)
    {
        int h = height(root);
        int i;
        List retVal = new List();
  
        for (i = 1; i <= h; i++) {
            List currentLevel = new List();
            var currentLevelOrder
                = getCurrentLevel(root, i, currentLevel)
                      .ToList();
            retVal.Add(Convert.ToInt32(
                string.Join("", currentLevelOrder), 2));
        }
        return retVal.ToArray();
    }
  
    // Compute the "height" of a tree --
    // the number of nodes along the longest
    // path from the root node down to the
    // farthest leaf node.
    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(0);
        tree.root.left = new Node(0);
        tree.root.right = new Node(1);
        tree.root.left.left = new Node(1);
        tree.root.right.left = new Node(0);
        tree.root.right.left.left = new Node(1);
        tree.root.right.left.right = new Node(1);
  
        bool ans = tree.checkMonotonic(tree.root);
        if (ans)
            Console.Write("Yes");
        else
            Console.Write("No");
    }
}


C#
// C# code to implement the 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 GFG {
    // Root of the Binary Tree
    public Node root;
  
    public virtual bool checkMonotonic(Node root)
    {
        int h = height(root);
        int i;
        int prevLevelValue
            = Convert.ToInt32(root.data.ToString(), 2);
  
        bool increasing = true;
        bool decreasing = true;
  
        for (i = 1; i <= h; i++) {
            List currentLevel = new List();
            var currentLevelOrder
                = getCurrentLevel(root, i, currentLevel)
                      .ToList();
            int currentLevelValue = Convert.ToInt32(
                string.Join("", currentLevelOrder), 2);
  
            if (prevLevelValue > currentLevelValue)
                increasing = false;
            if (prevLevelValue < currentLevelValue)
                decreasing = false;
  
            prevLevelValue = currentLevelValue;
        }
        return increasing || decreasing;
    }
  
    // Compute the "height" of a tree --
    // the number of nodes along the longest
    // path from the root node down to the
    // farthest leaf node.
    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(0);
        tree.root.left = new Node(0);
        tree.root.right = new Node(1);
        tree.root.left.left = new Node(1);
        tree.root.right.left = new Node(0);
        tree.root.right.left.left = new Node(1);
        tree.root.right.left.right = new Node(1);
  
        bool ans = tree.checkMonotonic(tree.root);
        if (ans)
            Console.Write("Yes");
        else
            Console.Write("No");
    }
}


输出
Yes

时间复杂度:O(N^2)
辅助空间:O(N+L),其中 L 是 Tree 中的层数,它将存储每个层的十进制值的 Array 的大小

高效方法:上述方法中的辅助空间可以通过查看相邻级别并检查它们是否遵循单调序列来优化。

我们可以在遍历每个级别时检查每个级别的递增/递减性质,而不是将每个级别的十进制等效值存储在数组中,然后检查数组是否单调。这样,一旦我们得到一个打破单调性的值,我们就可以终止。为此,我们需要存储上一级的值以与下一级进行比较。

下面是上述方法的实现:

C#

// C# code to implement the 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 GFG {
    // Root of the Binary Tree
    public Node root;
  
    public virtual bool checkMonotonic(Node root)
    {
        int h = height(root);
        int i;
        int prevLevelValue
            = Convert.ToInt32(root.data.ToString(), 2);
  
        bool increasing = true;
        bool decreasing = true;
  
        for (i = 1; i <= h; i++) {
            List currentLevel = new List();
            var currentLevelOrder
                = getCurrentLevel(root, i, currentLevel)
                      .ToList();
            int currentLevelValue = Convert.ToInt32(
                string.Join("", currentLevelOrder), 2);
  
            if (prevLevelValue > currentLevelValue)
                increasing = false;
            if (prevLevelValue < currentLevelValue)
                decreasing = false;
  
            prevLevelValue = currentLevelValue;
        }
        return increasing || decreasing;
    }
  
    // Compute the "height" of a tree --
    // the number of nodes along the longest
    // path from the root node down to the
    // farthest leaf node.
    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(0);
        tree.root.left = new Node(0);
        tree.root.right = new Node(1);
        tree.root.left.left = new Node(1);
        tree.root.right.left = new Node(0);
        tree.root.right.left.left = new Node(1);
        tree.root.right.left.right = new Node(1);
  
        bool ans = tree.checkMonotonic(tree.root);
        if (ans)
            Console.Write("Yes");
        else
            Console.Write("No");
    }
}
输出
Yes

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