标签:post mil gen nod 代码 lis rate public i++
代码例如以下:
public ArrayList<TreeNode> generateTrees(int n) { return helper(1,n); } private ArrayList<TreeNode> helper(int left, int right) { ArrayList<TreeNode> res = new ArrayList<TreeNode>(); if(left>right) { res.add(null); return res; } for(int i=left;i<=right;i++) { ArrayList<TreeNode> leftList = helper(left,i-1); ArrayList<TreeNode> rightList = helper(i+1,right); for(int j=0;j<leftList.size();j++) { for(int k=0;k<rightList.size();k++) { TreeNode root = new TreeNode(i); root.left = leftList.get(j); root.right = rightList.get(k); res.add(root); } } } return res; }实现中还是有一些细节的,由于构造树时两边要遍历全部左右的匹配。然后接到根上面。
Unique Binary Search Trees II -- LeetCode
标签:post mil gen nod 代码 lis rate public i++
原文地址:http://www.cnblogs.com/brucemengbm/p/6962068.html