# Accepted 56ms Python Bottom Up Solution, O(n), beats 97%..

• I return 4 variables:
Status (if it is a BST, True/False), nodesCount (how many nodes in the subtree), Floor(int), Ceiling(int)

``````# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
def largestBSTSubtree(self, root):
"""
:type root: TreeNode
:rtype: int
"""
if root is None:
return 0

def helper(node):
if node.left is None and node.right is None:
return True, 1, node.val, node.val

if node.left is None and node.right:
statusR, numR, rightFloor, rightCeiling = helper(node.right)
if statusR and node.val < rightFloor:
return True, numR + 1, node.val, rightCeiling
else:
return False, 1, node.val, rightCeiling

elif node.right is None and node.left:
statusL, numL, leftFloor, leftCeiling = helper(node.left)
if statusL and node.val > leftCeiling:
return True, numL + 1, leftFloor, node.val
else:
return False, 1, leftFloor, node.val
else:
statusL, numL, leftFloor, leftCeiling = helper(node.left)
statusR, numR, rightFloor, rightCeiling = helper(node.right)

if statusL and statusR and  leftCeiling < node.val < rightFloor: