File: root - text - article - 2019 - 11 - validate-binary-search-tree.txt.txt
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Category: Computing | 431 Views, 18228 Search Bots | 128 Words
| Browse | Archive
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Category: Computing | 431 Views, 18228 Search Bots | 128 Words
| Browse | Archive
Hi, here's your problem today. This problem was recently asked by Facebook:
You are given the root of a binary search tree. Return true if it is a valid binary search tree, and false otherwise. Recall that a binary search tree has the property that all values in the left subtree are less than or equal to the root, and all values in the right subtree are greater than or equal to the root.
Here's a starting point:
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Cateogry: Computing | 431 Views, 18228 Search Bots | 128 Words You are given the root of a binary search tree. Return true if it is a valid binary search tree, and false otherwise. Recall that a binary search tree has the property that all values in the left subtree are less than or equal to the root, and all values in the right subtree are greater than or equal to the root.
Here's a starting point:
class TreeNode:
def __init__(self, key):
self.left = None
self.right = None
self.key = key
def is_bst(root):
# Fill this in.
a = TreeNode(5)
a.left = TreeNode(3)
a.right = TreeNode(7)
a.left.left = TreeNode(1)
a.left.right = TreeNode(4)
a.right.left = TreeNode(6)
print is_bst(a)
# 5
# / \
# 3 7
# / \ /
#1 4 6
Related Articles
- Daily Interview Question: Find Cycles in a Graph
- Daily Interview Problem: Find the k-th Largest Element in a List
- Daily Interview Problem: Find the Number of Islands
- [Daily Problem] Course Prerequisites
- Daily Interview Problem: Given two arrays, write a function to compute their intersection.
- Daily Interview Question: Longest Sequence with Two Unique Numbers
- Daily Interview Question: Create a Simple Calculator
- Daily Interview Question: Edit Distance
- Sort a Partially Sorted List
- Daily Interview Problem: Trapping Rainwater
©2006~2024 SteakOverCooked - 0.00921 Seconds(s) - 5874.847 KB/s - 27 Online Memory: 516.38 KB
18:54:01 up 13 days, 18:33, 2 users, load average: 0.98, 0.86, 0.73 - Server PHP Version: 7.4.33
How to Cook a Perfect Steak? | <meta name="robots" content="noindex, follow" />
18:54:01 up 13 days, 18:33, 2 users, load average: 0.98, 0.86, 0.73 - Server PHP Version: 7.4.33
Comments (0)
Read & Write - Normal - Mini - Post - All Comments - Statistics
Be the first one to comment this page !