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)
        }
    }
  • 相关阅读:
    c语言的流程控制
    C语言基本用算
    C语言的scanf函数
    C语言一些知识点总结
    C语言程序
    House Robber 分类: leetcode 算法 2015-07-09 20:53 2人阅读 评论(0) 收藏
    [leetcode] Reverse Linked List 分类: leetcode 算法 2015-07-09 18:44 2人阅读 评论(0) 收藏
    合并k个已排序的链表 分类: leetcode 算法 2015-07-09 17:43 3人阅读 评论(0) 收藏
    [leetcode] Reverse Linked List
    Merge k Sorted Lists
  • 原文地址:https://www.cnblogs.com/Spider-spiders/p/10489011.html
Copyright © 2011-2022 走看看