检查二叉树的逐级十进制等价物是否形成单调序列
给定二叉树的根,其中所有节点的值都为0或1 ,任务是检查给定 Tree 的逐级十进制等值是否形成单调序列。
A sequence is monotonic if it is either monotone increasing or monotone decreasing.
A sequence nums is monotone increasing if for all i <= j, nums[i] <= nums[j].
A sequence nums is monotone decreasing if for all i <= j, nums[i] >= nums[j].
例子:
Input:
0
/ \
0 1
/ /
1 0
/ \
1 1
Output:
Yes
Explanation:
Level 0: 0 -> 0
Level 1: 01 -> 1
Level 2: 10 -> 2
Level 3: 11 -> 3
The sequence formed from the decimal equivalent of each level from root to leaf is {0, 1, 2, 3} which is a monotone increasing sequence.
Input:
1
/ \
1 1
/ /
1 0
/ \
0 1
Output:
No
方法:
The idea is to perform levelorder traversal of given Tree and for each level, convert the binary representation to decimal and store in an int variable. Then the problem will be converted to simply check if given array is a monotonic sequence or not.
请按照以下步骤解决此问题:
- 做从根到叶的水平顺序遍历
- 对于每个级别,将其转换为等效的十进制并将值存储在整数数组中
- 然后检查给定的数组是单调递增还是递减
下面是上述方法的实现:
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)