给定正整数的二叉树。任务是使用节点数字上的左移操作将其转换为BST 。如果无法将二进制树转换为BST,则打印-1 。
例子:
Input: 443
/ \
132 543
/ \
343 237
Output: 344
/ \
132 435
/ \
433 723
Explanation: 443 → left shift two times → 344
132 → zero shift operations → 132
543 → left shift one time → 435
343 → left shift one time → 433
237 → left shift two times → 723
Input: 43
/ \
56 45
Output: -1
方法:因为对BST进行有序遍历总是会生成排序的序列,所以这种想法是对二叉树进行有序遍历。请按照以下步骤解决问题:
- 初始化一个变量,例如prev = -1 ,以跟踪上一个节点的值。
- 然后,使用“有序遍历”遍历树,并尝试通过向左移动其数字来将每个节点转换为大于先前值的最低可能值。
- 执行完这些操作后,检查二进制树是否为BST 。
- 如果发现是真的,则打印树。否则,打印-1 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Structure of a Node
struct TreeNode
{
int val;
TreeNode *left, *right;
TreeNode(int key)
{
val = key;
left = NULL;
right = NULL;
}
};
// Function to check if
// the tree is BST or not
bool isBST(TreeNode *root, TreeNode *l = NULL, TreeNode *r = NULL)
{
// Base condition
if (!root)
return true;
// Check for left child
if (l && root->val <= l->val)
return false;
// Check for right child
if (r && root->val >= r->val)
return false;
// Check for left and right subtrees
return isBST(root->left, l, root) and isBST(root->right, root, r);
}
// Function to convert a tree to BST
// by left shift of its digits
void ConvTree(TreeNode *root,int &prev)
{
// If root is NULL
if (!root)
return;
// Recursively modify the
// left subtree
ConvTree(root->left,prev);
// Initialise optimal element
// to the initial element
int optEle = root->val;
// Convert it into a string
string strEle = to_string(root->val);
// For checking all the
// left shift operations
bool flag = true;
for (int idx = 0; idx < strEle.size(); idx++)
{
// Perform left shift
int shiftedNum = stoi(strEle.substr(idx) + strEle.substr(0, idx));
// If the number after left shift
// is the minimum and greater than
// its previous element
// first value >= prev
// used to setup initialize optEle
// cout<= prev and flag)
{
optEle = shiftedNum;
flag = false;
}
if (shiftedNum >= prev)
optEle = min(optEle, shiftedNum);
}
root->val = optEle;
prev = root->val;
// Recursively solve for right
// subtree
ConvTree(root->right,prev);
}
// Function to print level
// order traversal of a tree
void levelOrder(TreeNode *root)
{
queue que;
que.push(root);
while(true)
{
int length = que.size();
if (length == 0)
break;
while (length)
{
auto temp = que.front();
que.pop();
cout << temp->val << " ";
if (temp->left)
que.push(temp->left);
if (temp->right)
que.push(temp->right);
length -= 1;
}
cout << endl;
}
// new line
cout << endl;
}
// Driver Code
int main()
{
TreeNode *root = new TreeNode(443);
root->left = new TreeNode(132);
root->right = new TreeNode(543);
root->right->left = new TreeNode(343);
root->right->right = new TreeNode(237);
// Converts the tree to BST
int prev = -1;
ConvTree(root, prev);
// If tree is converted to a BST
if (isBST(root, NULL, NULL))
{
// Print its level order traversal
levelOrder(root);
}
else
{
// not possible
cout << (-1);
}
}
// This code is contributed by mohit kumar 29
Java
// Java program for the above approach
import java.util.*;
class GFG{
static int prev;
// Structure of a Node
static class TreeNode
{
int val;
TreeNode left, right;
TreeNode(int key)
{
val = key;
left = null;
right = null;
}
};
// Function to check if
// the tree is BST or not
static boolean isBST(TreeNode root, TreeNode l,
TreeNode r)
{
// Base condition
if (root == null)
return true;
// Check for left child
if (l != null && root.val <= l.val)
return false;
// Check for right child
if (r != null && root.val >= r.val)
return false;
// Check for left and right subtrees
return isBST(root.left, l, root) &
isBST(root.right, root, r);
}
// Function to convert a tree to BST
// by left shift of its digits
static void ConvTree(TreeNode root)
{
// If root is null
if (root == null)
return;
// Recursively modify the
// left subtree
ConvTree(root.left);
// Initialise optimal element
// to the initial element
int optEle = root.val;
// Convert it into a String
String strEle = String.valueOf(root.val);
// For checking all the
// left shift operations
boolean flag = true;
for(int idx = 0; idx < strEle.length(); idx++)
{
// Perform left shift
int shiftedNum = Integer.parseInt(
strEle.substring(idx) +
strEle.substring(0, idx));
// If the number after left shift
// is the minimum and greater than
// its previous element
// first value >= prev
// used to setup initialize optEle
// System.out.print(prev<= prev && flag)
{
optEle = shiftedNum;
flag = false;
}
if (shiftedNum >= prev)
optEle = Math.min(optEle, shiftedNum);
}
root.val = optEle;
prev = root.val;
// Recursively solve for right
// subtree
ConvTree(root.right);
}
// Function to print level
// order traversal of a tree
static void levelOrder(TreeNode root)
{
Queue que = new LinkedList();
que.add(root);
while(true)
{
int length = que.size();
if (length == 0)
break;
while (length > 0)
{
TreeNode temp = que.peek();
que.remove();
System.out.print(temp.val + " ");
if (temp.left != null)
que.add(temp.left);
if (temp.right != null)
que.add(temp.right);
length -= 1;
}
System.out.println();
}
// New line
System.out.println();
}
// Driver Code
public static void main(String[] args)
{
TreeNode root = new TreeNode(443);
root.left = new TreeNode(132);
root.right = new TreeNode(543);
root.right.left = new TreeNode(343);
root.right.right = new TreeNode(237);
// Converts the tree to BST
prev = -1;
ConvTree(root);
// If tree is converted to a BST
if (isBST(root, null, null))
{
// Print its level order traversal
levelOrder(root);
}
else
{
// not possible
System.out.print(-1);
}
}
}
// This code is contributed by shikhasingrajput
Python3
# Python3 program for the above approach
# Structure of a Node
class TreeNode:
def __init__(self, key):
self.val = key
self.left = None
self.right = None
# Function to check if
# the tree is BST or not
def isBST(root, l = None, r = None):
# Base condition
if not root:
return True
# Check for left child
if l and root.val <= l.val:
return False
# Check for right child
if r and root.val >= r.val:
return False
# Check for left and right subtrees
return isBST(root.left, l, root) and isBST(root.right, root, r)
# Function to convert a tree to BST
# by left shift of its digits
def ConvTree(root):
global prev
# If root is NULL
if not root:
return
# Recursively modify the
# left subtree
ConvTree(root.left)
# Initialise optimal element
# to the initial element
optEle = root.val
# Convert it into a string
strEle = str(root.val)
# For checking all the
# left shift operations
flag = True
for idx in range(len(strEle)):
# Perform left shift
shiftedNum = int(strEle[idx:] + strEle[:idx])
# If the number after left shift
# is the minimum and greater than
# its previous element
# first value >= prev
# used to setup initialize optEle
if shiftedNum >= prev and flag:
optEle = shiftedNum
flag = False
if shiftedNum >= prev:
optEle = min(optEle, shiftedNum)
root.val = optEle
prev = root.val
# Recursively solve for right
# subtree
ConvTree(root.right)
# Function to print level
# order traversal of a tree
def levelOrder(root):
que = [root]
while True:
length = len(que)
if not length:
break
while length:
temp = que.pop(0)
print(temp.val, end =' ')
if temp.left:
que.append(temp.left)
if temp.right:
que.append(temp.right)
length -= 1
# new line
print()
# Driver Code
root = TreeNode(443)
root.left = TreeNode(132)
root.right = TreeNode(543)
root.right.left = TreeNode(343)
root.right.right = TreeNode(237)
# Initialize to
# previous element
prev = -1
# Converts the tree to BST
ConvTree(root)
# If tree is converted to a BST
if isBST(root, None, None):
# Print its level order traversal
levelOrder(root)
else:
# not possible
print(-1)
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG
{
static int prev;
// Structure of a Node
class TreeNode
{
public int val;
public TreeNode left, right;
public TreeNode(int key)
{
val = key;
left = null;
right = null;
}
};
// Function to check if
// the tree is BST or not
static bool isBST(TreeNode root, TreeNode l,
TreeNode r)
{
// Base condition
if (root == null)
return true;
// Check for left child
if (l != null && root.val <= l.val)
return false;
// Check for right child
if (r != null && root.val >= r.val)
return false;
// Check for left and right subtrees
return isBST(root.left, l, root) &
isBST(root.right, root, r);
}
// Function to convert a tree to BST
// by left shift of its digits
static void ConvTree(TreeNode root)
{
// If root is null
if (root == null)
return;
// Recursively modify the
// left subtree
ConvTree(root.left);
// Initialise optimal element
// to the initial element
int optEle = root.val;
// Convert it into a String
String strEle = String.Join("", root.val);
// For checking all the
// left shift operations
bool flag = true;
for(int idx = 0; idx < strEle.Length; idx++)
{
// Perform left shift
int shiftedNum = Int32.Parse(
strEle.Substring(idx) +
strEle.Substring(0, idx));
// If the number after left shift
// is the minimum and greater than
// its previous element
// first value >= prev
// used to setup initialize optEle
// Console.Write(prev<= prev && flag)
{
optEle = shiftedNum;
flag = false;
}
if (shiftedNum >= prev)
optEle = Math.Min(optEle, shiftedNum);
}
root.val = optEle;
prev = root.val;
// Recursively solve for right
// subtree
ConvTree(root.right);
}
// Function to print level
// order traversal of a tree
static void levelOrder(TreeNode root)
{
Queue que = new Queue();
que.Enqueue(root);
while(true)
{
int length = que.Count;
if (length == 0)
break;
while (length > 0)
{
TreeNode temp = que.Peek();
que.Dequeue();
Console.Write(temp.val + " ");
if (temp.left != null)
que.Enqueue(temp.left);
if (temp.right != null)
que.Enqueue(temp.right);
length -= 1;
}
Console.WriteLine();
}
// New line
Console.WriteLine();
}
// Driver Code
public static void Main(String[] args)
{
TreeNode root = new TreeNode(443);
root.left = new TreeNode(132);
root.right = new TreeNode(543);
root.right.left = new TreeNode(343);
root.right.right = new TreeNode(237);
// Converts the tree to BST
prev = -1;
ConvTree(root);
// If tree is converted to a BST
if (isBST(root, null, null))
{
// Print its level order traversal
levelOrder(root);
}
else
{
// not possible
Console.Write(-1);
}
}
}
// This code is contributed by shikhasingrajput
344
132 435
433 723
时间复杂度: O(N)
辅助空间: O(N)