【LeetCode】95. 不同的二叉搜索树 II(JAVA)(非100%)

本文详细解析了LeetCode第95题“不同的二叉搜索树II”的解题思路及代码实现,通过递归构造所有可能的二叉搜索树,并解决引用指向内容更新的问题,避免错误地修改已构造的树结构。

原题地址:https://leetcode-cn.com/problems/unique-binary-search-trees-ii/

题目描述:

给定一个整数 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

解题方案:

和96题的思路差不多,只不过改用递归构造树了。刚开始做错的有一点是没有考虑到更改引用指向的内容的问题,需要每次都新申请一个空间,否则下一次循环更改指向内容时上一个也跟着改了。

相关文章:【LeetCode】96. 不同的二叉搜索树(JAVA)

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public List<TreeNode> generateTrees(int n) {
        List<TreeNode> tree = new LinkedList<TreeNode>();
        if(n == 0)
            return tree;
        tree = rec_Trees(1, n);
        return tree;
    }

    public List<TreeNode> rec_Trees(int a, int b) {
        List<TreeNode> tree = new LinkedList<TreeNode>();
        if(a > b)
            return null;
        else if(a == b)
        {
            TreeNode t = new TreeNode(a);
            tree.add(t);
            return tree;
        }
        else
        {
            for(int k = a; k <= b; k ++)
            {
                //当k为根节点时     
                List<TreeNode> lo = rec_Trees(a, k - 1);
                List<TreeNode> hi = rec_Trees(k + 1, b);
                if(k == a)
                {
                    for(int j = 0; j < hi.size(); j ++)
                    {
                        TreeNode t = new TreeNode(k);
                        t.right = hi.get(j);
                        tree.add(t);
                    }
                }
                else if(k == b)
                {
                    for(int i = 0; i < lo.size(); i ++)
                    {
                        TreeNode t = new TreeNode(k);
                        t.left = lo.get(i);
                        tree.add(t);
                    }
                }
                else
                {
                    for(int i = 0; i < lo.size(); i ++)
                    {                   
                        for(int j = 0; j < hi.size(); j ++)
                        {
                            TreeNode t = new TreeNode(k);
                            t.left = lo.get(i);
                            t.right = hi.get(j);
                            tree.add(t);
                        }
                    }
                } 
            }
            return tree;
        }
    }
}

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值