Leetcode 95. Unique Binary Search Trees II

Input: n = 3
Output: [[1,null,2,null,3],[1,null,3,2],[2,1,3],[3,1,null,null,2],[3,2,null,1]]
Input: n = 1
Output: [[1]]
  • 1 <= n <= 8
  1. Think: will need a arraylist for the answer(TreeNode in the list)
  2. will need to use recursive to do the in-order traversal
  3. generate left subtree and right subtree, add them with the root, and recursive multiple situation.
  4. Trick: left = genTrees(start, i-1), right = genTrees(i+1, end)

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Gary Chiang

Gary Chiang

CS new grad, 6 years experience related to supply chain management. Located in Bay area