Java for LeetCode 095 Unique Binary Search Trees II

Given n, generate all structurally unique BST‘s (binary search trees) that store values 1...n.

For example,
Given n = 3, your program should return all 5 unique BST‘s shown below.

解题思路:

参考Java for LeetCode 096 Unique Binary Search Trees思路,本题很容易解决。注意,如果试图不复制而使用对象的引用,是无法得到正确结果的,当然也可采用DFS的思路,但是考虑到JAVA面向对象的特征,我们还是采用Java for LeetCode 096 Unique Binary Search Trees的思路吧。

JAVA实现如下:

static public List<TreeNode> generateTrees(int n) {
		List<TreeNode> list = new ArrayList<TreeNode>();
		if (n == 0) {
			list.add(null);
			return list;
		}
		if (n == 1) {
			list.add(new TreeNode(1));
			return list;
		}
		for (int k = 0; k <= n - 1; k++) {
			List<TreeNode> left = generateTrees(k);
			List<TreeNode> right = generateTrees(n - k - 1);
			for (TreeNode rightNode : right)
				plus(rightNode, k + 1);
			for (TreeNode leftNode : left) {
				for (TreeNode rightNode : right) {
					TreeNode TN = new TreeNode(k + 1);
					TN.left = treeNodeClone(leftNode);
					TN.right = treeNodeClone(rightNode);
					list.add(TN);
				}
			}
		}
		return list;
	}
	public static void plus(TreeNode root, int num) {
		if (root == null)
			return;
		root.val += num;
		plus(root.left, num);
		plus(root.right, num);
	}
	public static TreeNode treeNodeClone(TreeNode TN1){
		if(TN1==null)
			return null;
		TreeNode TN2=new TreeNode(TN1.val);
		TN2.left=treeNodeClone(TN1.left);
		TN2.right=treeNodeClone(TN1.right);
		return TN2;
	}
时间: 2024-11-03 20:45:39

Java for LeetCode 095 Unique Binary Search Trees II的相关文章

[LeetCode][JavaScript]Unique Binary Search Trees II

Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example,Given n = 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1

【LeetCode】Unique Binary Search Trees II 不同的二叉查找树II

今早起来做 LeetCode,结果被这道题卡了将近1个半小时,忍着没有去搜答案,最后还是被我想出来了,而且直接一次AC,哈哈!现在在这里记录一下解题思路. 原题: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example,Given n = 3, your program should return all 5 unique BST's sh

[C++]LeetCode: 92 Unique Binary Search Trees II

题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example, Given n = 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 confused what "{1,#,

Java for LeetCode 096 Unique Binary Search Trees

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. 解题思路: 二叉查找树,刚上来无从下手,但仔细想想就能发现规律:以i为根节点,i的左子树都是小于i的,共有numTrees(i-1)种情况,i的右子树都是大于i的,共有numTrees(n-i

【Leetcode】Unique Binary Search Trees II

题目链接:https://leetcode.com/problems/unique-binary-search-trees-ii/ 题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example, Given n = 3, your program should return all 5 unique BST's shown below.

leetCode 95.Unique Binary Search Trees II (唯一二叉搜索树) 解题思路和方法

Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example, Given n = 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 confused what "{1,#,2,3}

【leetcode】 Unique Binary Search Trees II (middle)☆

Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example,Given n = 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 这次的题目要求是得到所有的树. 我的思路: 用f[

Unique Binary Search Trees II leetcode java

题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example, Given n = 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 题解:这道题比1难的就是不是返回个数,而

[LeetCode] Unique Binary Search Trees II (难以忍受的递归)

Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example, Given n = 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 class Solution { private