Finding the largest subtree in a BST

前端 未结 9 763
别那么骄傲
别那么骄傲 2020-12-28 11:02

Given a binary tree, I want to find out the largest subtree which is a BST in it.

Naive approach:

I have a naive approach in mind where I visit every node of

9条回答
  •  不知归路
    2020-12-28 11:28

    This isn't the most optimal approach but you could do an inorder traversal of the Binary tree and store it in an array and then find the longest contiguous increasing sequence, that will give you the BST with the maximum number of nodes. The complexity would be O(n) for the traversal and O(n) for the search so it's still going to be O(n).

提交回复
热议问题