给定一棵二叉树,任务是检查给定的二叉树是否是二叉搜索树。如果发现是真的,则打印“YES” 。否则,打印“NO” 。
例子:
Input:
Output: YES
Explanation: Since the left subtree of each node of the tree consists of smaller valued nodes and the right subtree of each node of the tree consists of larger valued nodes. Therefore, the required is output is “YES”.
Input:
Output: NO
递归方法:参考上一篇文章,使用递归解决这个问题。
时间复杂度: O(N),其中 N 是二叉树中的节点数。
辅助空间: O(N)
迭代方法:要迭代解决问题,请使用堆栈。请按照以下步骤解决问题:
- 初始化堆栈以存储节点及其左子树。
- 初始化一个变量,比如prev ,以存储二叉树之前访问过的节点。
- 遍历树,将每个节点的根节点和左子树入栈,检查prev节点的数据值是否大于等于当前访问节点的数据值。如果发现是真的,则打印“NO” 。
- 否则,打印“YES” 。
下面是上述方法的实现。
C++
9
/ \
6 10
/ \ \
4 7 11
/ \ \
3 5 8
Java
5
/ \
6 3
/ \ \
4 9 2
Python3
// C++ program to implement
// the above approach
#include
using namespace std;
// Structure of a tree node
struct TreeNode {
// Stores data value of a node
int data;
// Stores left subtree
// of a tree node
TreeNode* left;
// Stores right subtree
// of a tree node
TreeNode* right;
// Initialization of
// a tree node
TreeNode(int val)
{
// Update data
data = val;
// Update left and right
left = right = NULL;
}
};
// Function to check if a binary tree
// is binary search tree or not
bool checkTreeIsBST(TreeNode *root)
{
// Stores root node and left
// subtree of each node
stack Stack;
// Stores previous visited node
TreeNode* prev = NULL;
// Traverse the binary tree
while (!Stack.empty() ||
root != NULL) {
// Traverse left subtree
while (root != NULL) {
// Insert root into Stack
Stack.push(root);
// Update root
root = root->left;
}
// Stores top element of Stack
root = Stack.top();
// Remove the top element of Stack
Stack.pop();
// If data value of root node less
// than data value of left subtree
if(prev != NULL &&
root->data <= prev->data) {
return false;
}
// Update prev
prev = root;
// Traverse right subtree
// of the tree
root = root->right;
}
return true;
}
// Driver Code
int main()
{
/*
9
/ \
6 10
/ \ \
4 7 11
/ \ \
3 5 8
*/
// Initialize binary tree
TreeNode *root = new TreeNode(9);
root->left = new TreeNode(6);
root->right = new TreeNode(10);
root->left->left = new TreeNode(4);
root->left->right = new TreeNode(7);
root->right->right = new TreeNode(11);
root->left->left->left = new TreeNode(3);
root->left->left->right = new TreeNode(5);
root->left->right->right = new TreeNode(8);
if (checkTreeIsBST(root)) {
cout<<"YES";
}
else {
cout<<"NO";
}
}
C#
// Java program to implement
// the above approach
import java.util.*;
class GFG{
// Structure of a tree node
static class TreeNode {
// Stores data value of a node
int data;
// Stores left subtree
// of a tree node
TreeNode left;
// Stores right subtree
// of a tree node
TreeNode right;
// Initialization of
// a tree node
TreeNode(int val)
{
// Update data
data = val;
// Update left and right
left = right = null;
}
};
// Function to check if a binary tree
// is binary search tree or not
static boolean checkTreeIsBST(TreeNode root)
{
// Stores root node and left
// subtree of each node
Stack Stack = new Stack();
// Stores previous visited node
TreeNode prev = null;
// Traverse the binary tree
while (!Stack.isEmpty() ||
root != null) {
// Traverse left subtree
while (root != null) {
// Insert root into Stack
Stack.add(root);
// Update root
root = root.left;
}
// Stores top element of Stack
root = Stack.peek();
// Remove the top element of Stack
Stack.pop();
// If data value of root node less
// than data value of left subtree
if(prev != null &&
root.data <= prev.data) {
return false;
}
// Update prev
prev = root;
// Traverse right subtree
// of the tree
root = root.right;
}
return true;
}
// Driver Code
public static void main(String[] args)
{
/*
9
/ \
6 10
/ \ \
4 7 11
/ \ \
3 5 8
*/
// Initialize binary tree
TreeNode root = new TreeNode(9);
root.left = new TreeNode(6);
root.right = new TreeNode(10);
root.left.left = new TreeNode(4);
root.left.right = new TreeNode(7);
root.right.right = new TreeNode(11);
root.left.left.left = new TreeNode(3);
root.left.left.right = new TreeNode(5);
root.left.right.right = new TreeNode(8);
if (checkTreeIsBST(root)) {
System.out.print("YES");
}
else {
System.out.print("NO");
}
}
}
// This code is contributed by Amit Katiyar
输出:
# Python3 program to implement
# the above approach
# Structure of a tree node
class TreeNode:
def __init__(self, data: int) -> None:
# Stores data value of a node
self.data = data
# Stores left subtree
# of a tree node
self.left = None
# Stores right subtree
# of a tree node
self.right = None
# Function to check if a binary tree
# is binary search tree or not
def checkTreeIsBST(root: TreeNode) -> bool:
# Stores root node and left
# subtree of each node
Stack = []
# Stores previous visited node
prev = None
# Traverse the binary tree
while (Stack or root):
# Traverse left subtree
while root:
# Insert root into Stack
Stack.append(root)
# Update root
root = root.left
# Stores top element of Stack
# Remove the top element of Stack
root = Stack.pop()
# If data value of root node less
# than data value of left subtree
if (prev and root.data <= prev.data):
return False
# Update prev
prev = root
# Traverse right subtree
# of the tree
root = root.right
return True
# Driver Code
if __name__ == "__main__":
'''
9
/ \
6 10
/ \ \
4 7 11
/ \ \
3 5 8
'''
# Initialize binary tree
root = TreeNode(9)
root.left = TreeNode(6)
root.right = TreeNode(10)
root.left.left = TreeNode(4)
root.left.right = TreeNode(7)
root.right.right = TreeNode(11)
root.left.left.left = TreeNode(3)
root.left.left.right = TreeNode(5)
root.left.right.right = TreeNode(8)
if checkTreeIsBST(root):
print("YES")
else:
print("NO")
# This code is contributed by sanjeev2552
时间复杂度: O(N),其中 N 是二叉树中的节点数
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live