File: root - text - article - 2019 - 12 - largest-bst-in-a-binary-tree.txt.txt
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Category: Computing | 416 Views, 22223 Search Bots | 118 Words
| Browse | Archive
![Subscribe to Feed Burner Subscribe to Feed Burner](/images/form/rss.gif)
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Category: Computing | 416 Views, 22223 Search Bots | 118 Words
![Subscribe to Feed Burner Subscribe to Feed Burner](/images/form/rss.gif)
Hi, here's your problem today. This problem was recently asked by Twitter:
You are given the root of a binary tree. Find and return the largest subtree of that tree, which is a valid binary search tree.
Here's a starting point:
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Cateogry: Computing | 416 Views, 22223 Search Bots | 118 Words You are given the root of a binary tree. Find and return the largest subtree of that tree, which is a valid binary search tree.
Here's a starting point:
class TreeNode:
def __init__(self, key):
self.left = None
self.right = None
self.key = key
def __str__(self):
# preorder traversal
answer = str(self.key)
if self.left:
answer += str(self.left)
if self.right:
answer += str(self.right)
return answer
def largest_bst_subtree(root):
# Fill this in.
# 5
# / \
# 6 7
# / / \
# 2 4 9
node = TreeNode(5)
node.left = TreeNode(6)
node.right = TreeNode(7)
node.left.left = TreeNode(2)
node.right.left = TreeNode(4)
node.right.right = TreeNode(9)
print largest_bst_subtree(node)
#749
![Subscribe to Feed Burner Subscribe to Feed Burner](/images/form/rss.gif)
Related Articles
- Algorithm Interview: Determine If Linked List is Palindrome
- Making a Height Balanced Binary Search Tree
- [Daily Problem] Remove Consecutive Nodes that Sum to 0
- Daily Interview Problem: 3 Sum
- Algorithm Interview Question: Nth Fibonacci Number
- Two Tricks of Delphi
- Daily Interview Problem: First Missing Positive Integer
- Daily Interview Problem: Look and Say Sequence
- Consecutive Ones
- How to Play WAV music under DOS?
©2006~2024 SteakOverCooked - 0.0094 Seconds(s) - 2155.377 KB/s - 14 Online
Memory: 492.83 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" />
![](/images/top.jpg)
18:54:01 up 13 days, 18:33, 2 users, load average: 0.98, 0.86, 0.73 - Server PHP Version: 7.4.33
Read & Write - Normal - Mini - Post - All Comments - Statistics
Be the first one to comment this page !