zoukankan      html  css  js  c++  java
  • 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.

    思路:

      dfs或者动态规划

    我的代码:

    public class Solution {
        public List<TreeNode> generateTrees(int n) {
            int[] num = new int[n];
            for(int i = 0; i < n; i++)
            {
                num[i] = i + 1;
            }
            return findNode(num, 0, n);
        }
        public List<TreeNode> findNode(int[] num, int start, int end)
        {
            if(start == end)
            {
                List<TreeNode> list = new ArrayList<TreeNode>();
                list.add(null);
                return list;
            }
            if(start + 1 == end) 
            {
                List<TreeNode> list = new ArrayList<TreeNode>();
                list.add(new TreeNode(num[start]));
                return list;
            }
            List<TreeNode> rst = new ArrayList<TreeNode>();
            for(int i = start; i < end; i++)
            {
               
                List<TreeNode> left = findNode(num, start, i);
                List<TreeNode> right = findNode(num, i + 1, end);
                for(TreeNode l: left)
                {
                    for(TreeNode r: right)
                    {
                        TreeNode root = new TreeNode(num[i]);
                        root.left = l;
                        root.right = r;
                        rst.add(root);
                    }
                }
            }
            return rst;
        }
    }
    View Code

    他人代码:

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    public class Solution {
    public List<TreeNode> generateTrees(int n) {
        Map<Integer, List<TreeNode>> lists = new HashMap<Integer, List<TreeNode>>();
    
        List<TreeNode> list = new LinkedList<TreeNode>();
        list.add(null);
        if (n==0) return list;
        lists.put(0, list);
    
        list = new LinkedList<TreeNode>();
        TreeNode root = new TreeNode(1);
        list.add(root);
        lists.put(1, list);
    
        for (int i=2; i<=n; i++) {
            list = new LinkedList<TreeNode>();
            for (int j=1; j<=i; j++) {
                for (TreeNode left:lists.get(j-1)) {
                    for (TreeNode right:lists.get(i-j)) {
                        root = new TreeNode(j);
                        root.left = left;
                        root.right = greaterCopy(right, j);
                        list.add(root);
                    }
                }
            }
            lists.put(i, list);
        }
        return list;
    }
    
    private TreeNode greaterCopy(TreeNode node, int add) {
        if (node == null) return null;
        TreeNode copy = new TreeNode(node.val + add);
        copy.left = greaterCopy(node.left, add);
        copy.right = greaterCopy(node.right, add);
        return copy;
    }
    }
    View Code

    学习之处:

    • DFS可以解决的问题往往用动态规划也可以解决,对于考虑Size的动态规划问题,往往是两层循环
    for(int size=1; size<=n; size++)
    {
          for(int j=0; j<=size; j++)
          {
           }   
    }    
    • 需要每一次重新 新建一个TreeNode root = new TreeNode(num[i])因为是地址啊,地址啊!这一点一定要注意。
  • 相关阅读:
    sublime manjaro
    codeforce 1027C 贪心算法
    codeforce B
    java生成随机数保留数点后两位
    form表单无限提交的问题
    Jquery TimePicker时间箭头无法加载的问题(http://localhost:8080/css/images/ui-icons_444444_256x240.png)
    Mysql 创建事件时出现near‘DO create event...’错误
    线程之实现
    redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool at redis.clients.util.Pool.getResource(Pool.java:53)
    Recursion递归
  • 原文地址:https://www.cnblogs.com/sunshisonghit/p/4340239.html
Copyright © 2011-2022 走看看