zoukankan      html  css  js  c++  java
  • leetcode 95. Unique Binary Search Trees II

    实现思路:分治:先建立左子树,再建立右子树,然后合并两种情况。

    /**
     * Definition for a binary tree node.
     * type TreeNode struct {
     *     Val int
     *     Left *TreeNode
     *     Right *TreeNode
     * }
     */
    func generateTrees(n int) []*TreeNode {
        if n < 1 {
            return nil
        }
        return createTrees(1, n)
    }
    
    func createTrees(begin int, end int) []*TreeNode {
        var trees []*TreeNode
        if begin > end {
            trees = append(trees, nil)
        }
        for i := begin; i <= end; i++ {
            leftTrees := createTrees(begin, i-1)
            rightTrees := createTrees(i+1, end)
            for _, left := range leftTrees {
                for _, right := range rightTrees {
                    root := TreeNode{i, left, right}
                    trees = append(trees, &root)
                }
            }
        }
        return trees
    }

    由于左子树和右子树是相互独立的子问题,那么其实可以同时进行,使用goroutine来实现:

    package main
    
    import "fmt"
    
    // Definition for a binary tree node.
    type TreeNode struct {
        Val   int
        Left  *TreeNode
        Right *TreeNode
    }
    
    func generateTrees(n int) []*TreeNode {
        begin := make(chan int, 1)
        end := make(chan int, 1)
        result := make(chan []*TreeNode)
        // var resultArr []*TreeNode
        if n < 1 {
            return nil
        }
        begin <- 1
        end <- n
        go createTrees(begin, end, result)
        myresult := <-result
        return myresult
    }
    
    func createTrees(begin chan int, end chan int, treesChan chan []*TreeNode) {
        // var trees []*TreeNode
        leftTrees := make(chan []*TreeNode)
        rightTrees := make(chan []*TreeNode)
        mid := make(chan int, 1)
        var rootset, leftTreesR, rightTreesR []*TreeNode
        b := <-begin
        e := <-end
        if b > e {
            rootset = append(rootset, nil)
        }
    
        for i := b; i <= e; i++ {
            mid <- (i - 1)
            begin <- b
            go createTrees(begin, mid, leftTrees)
            mid <- (i + 1)
            end <- e
            go createTrees(mid, end, rightTrees)
            leftTreesR = <-leftTrees
            rightTreesR = <-rightTrees
            for _, left := range leftTreesR {
                for _, right := range rightTreesR {
                    root := TreeNode{i, left, right}
                    rootset = append(rootset, &root)
                }
            }
        }
        treesChan <- rootset
    }
    
    func main() {
        trees := generateTrees(20)
        for _, value := range trees {
            fmt.Println(value)
        }
    }
  • 相关阅读:
    【机器学习】--xgboost初始之代码实现分类
    【机器学习】--xgboost从初识到应用
    【机器学习篇】--SVD从初始到应用
    【深度学习篇】--Seq2Seq模型从初识到应用
    【神经网络篇】--RNN递归神经网络初始与详解
    【机器学习】--FP-groupth算法从初始到应用
    JS Date对象
    实现表单input文本框不可编辑的三种方法
    Java 书写规范简单整理
    C3P0数据库连接池数据库插入中文乱码问题解决
  • 原文地址:https://www.cnblogs.com/Spider-spiders/p/10489011.html
Copyright © 2011-2022 走看看