首页
题库
面试
求职
学习
竞赛
More+
所有博客
搜索面经/职位/试题/公司
搜索
我要招人
去企业版
登录 / 注册
首页
>
试题广场
>
不同的二叉搜索树 ii
[编程题]不同的二叉搜索树 ii
热度指数:10152
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32M,其他语言64M
算法知识视频讲解
给定一个值n,请生成
所有的
存储值
1...n.的
二叉搜索树
(
BST)
的结构
例如:
给定n=3,你的程序应该给出下面五种不同的二叉搜索树(
BST)
示例1
输入
3
输出
[{1,#,2,#,3},{1,#,3,2},{2,1,3},{3,1,#,#,2},{3,2,#,1}]
说明:本题目包含复杂数据结构TreeNode,
点此查看相关信息
马上挑战
算法知识视频讲解
提交运行
算法知识视频讲解
添加笔记
求解答(12)
邀请回答
收藏(146)
分享
提交结果有问题?
39个回答
3篇题解
开通博客
O-Precedence
发表于 2021-09-09 15:51:54
判断函数终止时,null值加入到res中很关键。 选定某个根节点时,递归得到他所有可能的左子树和右子树。 同一根节点下的左子树和右子树可以任意组合。 得到最后的res。 public class Solution { public ArrayList<TreeNode> gen
展开全文
华科不平凡
发表于 2020-08-23 03:32:36
后序遍历的变体,先将左右子树的所有搭配方式到两个vector,然后再用根节点分别与左右子树搭配: // // Created by jt on 2020/8/23. // #include <vector> using namespace std; class Solution {
展开全文
一叶浮尘
发表于 2020-04-11 13:06:30
给定一个值n,请生成所有的存储值1...n.的二叉搜索树(BST)的结构例如:给定n=3,你的程序应该给出下面五种不同的二叉搜索树(BST) /** * Definition for binary tree * public class TreeNode { * int val;
展开全文
问题信息
树
难度:
39条回答
146收藏
14515浏览
热门推荐
通过挑战的用户
查看代码
牛客51536...
2022-09-17 00:10:41
Varus20...
2022-08-20 14:50:14
阿阿
2022-08-05 16:26:23
Lilisten
2022-07-31 12:16:05
DamonGuan
2022-07-06 23:46:39
相关试题
未来工作城市的选择是怎样的?
通用能力
评论
(1)
你说在销售运营这个岗位上会涉及到一...
评论
(1)
“乔布斯不做调查,张小龙不看数据。...
用户研究
评论
(1)
相关性分析有哪些?
评论
(1)
如何检验聚类分析结果
评论
(1)
扫描二维码,关注牛客网
意见反馈
下载牛客APP,随时随地刷题
import java.util.*; /* * public class TreeNode { * int val = 0; * TreeNode left = null; * TreeNode right = null; * } */ public class Solution { /** * * @param n int整型 * @return TreeNode类ArrayList */ public ArrayList
generateTrees (int n) { // write code here } }
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ class Solution { public: /** * * @param n int整型 * @return TreeNode类vector */ vector
generateTrees(int n) { // write code here } };
# class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # # # @param n int整型 # @return TreeNode类一维数组 # class Solution: def generateTrees(self , n ): # write code here
/* * function TreeNode(x) { * this.val = x; * this.left = null; * this.right = null; * } */ /** * * @param n int整型 * @return TreeNode类一维数组 */ function generateTrees( n ) { // write code here } module.exports = { generateTrees : generateTrees };
# class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # # # @param n int整型 # @return TreeNode类一维数组 # class Solution: def generateTrees(self , n ): # write code here
package main import . "nc_tools" /* * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ /** * * @param n int整型 * @return TreeNode类一维数组 */ func generateTrees( n int ) []*TreeNode { // write code here }
3
[{1,#,2,#,3},{1,#,3,2},{2,1,3},{3,1,#,#,2},{3,2,#,1}]