Leetcode - Unique Binary Search Trees

题注

这道题本身不难,不过这次涉及到一些数学分析,很基础。LeetCode的题目本身都比较基础,但是正如《灌篮高手》里面赤木刚宪对樱木花道说的:基础最重要。没有掌握好的代码基础,剩下的一切都是扯淡。

题目

Given n, how many structurally unique BST's (binary search trees) that store values 1...n?

For example,
Given n = 3, there are a total of 5 unique BST's.

   1         3     3      2      1
    \       /     /      / \      \
     3     2     1      1   3      2
    /     /       \                 \
   2     1         2                 3

补充知识:Binary Search Trees

A binary search tree (BST), sometimes also called anordered orsorted binary tree, is a node-based binary data structure which has the following properties:

  • 1. The left subtree of a node contains only nodes with keys less than the node's key.
  • 2. The right subtree of a node contains only nodes with keys greater than the node's key.
  • 3. The left and right subtree each must also be a binary search tree.
  • 4. There must be no duplicate nodes.

分析

Binary Search Tree的最大特点是节点左子树所有节点值小于节点值本身;又子树所有节点值大于节点值本身。Binary Search Tree也有递归特性,因此自然而然地,这道题用递归特性(更确切的说,分治方法)来做最为合适。

如何递归呢?我们设函数$numTrees(n)$为存储了$[1, n]$的树中,Binary Search Tree的数量。显然地,我们有:

\[numTrees(0) = 0\]

\[numTrees(1) = 1\]

当$n \geq 2$时,我们分别将root节点的值设为$1,2, \cdots n$。当root节点值为$i \in [1, n]$时,其左子树的值必然为全部的$[1, i - 1]$(由性质1所确定),其右子树的值必然为全部的$[i+1, n]$(由性质2所确定)。同时,左右子树又必然为Binary Search Tree(由性质3所决定)。因此,左子树便成为了一个numTrees(i - 1)问题,右子树便称为了一个numTrees(n - 1 - i)问题。形式化的说,我们有,对于所有的$n \geq 2$:

\[numTrees(n) = numTrees(0) \cdot numTrees(n-1) + numTrees(1) \cdot numTrees(n-2) + \cdots + numTrees(n-1) \codt numTrees(0)\]

问题就迎刃而解了。

不过,为了简单地递归,我们这里设为$numTrees(0) = 1$,这样$numTrees(0) \cdot numTrees(n-1)$便不为$0$了,方便进行处理。

代码

public class Solution {
    public int reverse(int x) {
        boolean isNeg;
        if (x < 0){
            isNeg = true;
            x = -1 * x;
        }else{
            isNeg = false;
        }
        int reverResult = 0;
        while (x != 0){
            reverResult = reverResult * 10 + x % 10;
            x = x / 10;
        }
        if (isNeg){
            reverResult = -1 * reverResult;
        }
        return reverResult;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
LeetCode-Editor是一种在线编码工具,它提供了一个用户友好的界面编写和运行代码。在使用LeetCode-Editor时,有时候会出现乱码的问题。 乱码的原因可能是由于编码格式不兼容或者编码错误导致的。在这种情况下,我们可以尝试以下几种解决方法: 1. 检查文件编码格式:首先,我们可以检查所编辑的文件的编码格式。通常来说,常用的编码格式有UTF-8和ASCII等。我们可以将编码格式更改为正确的格式。在LeetCode-Editor中,可以通过界面设置或编辑器设置来更改编码格式。 2. 使用正确的字符集:如果乱码是由于使用了不同的字符集导致的,我们可以尝试更改使用正确的字符集。常见的字符集如Unicode或者UTF-8等。在LeetCode-Editor中,可以在编辑器中选择正确的字符集。 3. 使用合适的编辑器:有时候,乱码问题可能与LeetCode-Editor自身相关。我们可以尝试使用其他编码工具,如Text Editor、Sublime Text或者IDE,看是否能够解决乱码问题。 4. 查找特殊字符:如果乱码问题只出现在某些特殊字符上,我们可以尝试找到并替换这些字符。通过仔细检查代码,我们可以找到导致乱码的特定字符,并进行修正或替换。 总之,解决LeetCode-Editor乱码问题的方法有很多。根据具体情况,我们可以尝试更改文件编码格式、使用正确的字符集、更换编辑器或者查找并替换特殊字符等方法来解决这个问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值