Skip to content

Latest commit

 

History

History
82 lines (63 loc) · 1.32 KB

[0095] 不同的二叉搜索树 II.md

File metadata and controls

82 lines (63 loc) · 1.32 KB
title tags categories author comments updated permalink mathjax top description date
[0095] 不同的二叉搜索树 II
leetcode
leetcode
张学志
true
false
false
false
...
2019-12-31 16:01:35 -0800

题目描述

给定一个整数 n,生成所有由 1 ... n 为节点所组成的二叉搜索树

示例:

输入: 3
输出:
[
  [1,null,3,2],
  [3,2,null,1],
  [3,1,null,null,2],
  [2,1,3],
  [1,null,2,null,3]
]
解释:
以上的输出对应以下 5 种不同结构的二叉搜索树:

   1         3     3      2      1
    \       /     /      / \      \
     3     2     1      1   3      2
    /     /       \                 \
   2     1         2                 3
Related Topics
  • 动态规划
  • 题目代码

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
        vector<TreeNode*> generateTrees(int n) {
    
        }
    };

    题目解析

    方法一

    方法二

    方法三