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])因为是地址啊,地址啊!这一点一定要注意。
  • 相关阅读:
    项目spring boot 写es hbase 运行内存溢出
    spring boot项目启动报错
    线程的创建启动及线程池的使用
    ajax 跨域问题处理
    spring @Value("${name}")使用
    平时服务正常,突然挂了,怎么重启都起不来,查看日志Insufficient space for shared memory file 内存文件空间不足
    oracle 特殊符号替换删除处理
    Linux——CentOS 7 systemctl和防火墙firewalld命令
    linux 查看并对外开放端口(防火墙拦截处理)
    SpringBoot 使用 Gson 序列化(禁用 Jackson)
  • 原文地址:https://www.cnblogs.com/sunshisonghit/p/4340239.html
Copyright © 2011-2022 走看看