zoukankan      html  css  js  c++  java
  • LeetCode_Unique Binary Search Trees II

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n.
    
    For example,
    Given n = 3, your program should return all 5 unique BST's shown below.
       1         3     3      2      1
               /     /      /       
         3     2     1      1   3      2
        /     /                        
       2     1         2                 3
    

    转自: http://yucoding.blogspot.com/2013/05/leetcode-question115-unique-binary.html  

    Analysis:

    The basic idea is still using the DFS scheme. It is a little hard to think the structure of the argument list in the function. It is clear that for each tree/subtree, we will set the root as the start position to the end position, and recursively construct the left subtree with the left part and the right subtree with the right part.
    So first we can have
     void dfs (int st, int ed     ){
       if (st>ed) {   // generate a null node }
      else{
        for (int i=st;i<=ed;i++){
            
          dfs(st,i-1,   );     //generate left subtree 
          dfs(i+1,ed,   );  // generate right subtree
        }
      }

    }

    Next step is to think about how to store all the possible solutions.
    This is important ! Think about the root node, all the possible solutions of the tree are from the combinations of all the possible solutions of its left subtree, and its right subtree. One step further, if we have a root node and a left node, for the left node, still the subtrees below it are the combinations of the possible solutions of its left and right subtree, until the leaf node.

    In other words, we store all the possible solutions for each node, instead of storing the only tree. So, we can have
    void dfs(int st, int ed, vector<TreeNode *> res){}
    in this function, recursively generate the left tree and right tree, then construct the current node, and push it to the current solution vector.
    Details see the code.

    /**
     * Definition for binary tree
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
       void dfs(vector<TreeNode *> &res, int left , int right){
           if(left > right){
                res.push_back(NULL);
                return ;
           }
           
           for(int k = left; k <= right ; k++)
           {    
                vector<TreeNode *> leftTree;
                dfs(leftTree, left, k-1);
                vector<TreeNode *> rightTree;
                dfs(rightTree, k+1, right);
                for(int i = 0; i < leftTree.size(); i++)
                    for( int j = 0; j < rightTree.size(); j++)
                        {
                            TreeNode *root = new TreeNode(k);
                            root->left = leftTree[i];
                            root->right = rightTree[j];
                            res.push_back(root);
                        }
           }
       }
        vector<TreeNode *> generateTrees(int n) {
            // Start typing your C/C++ solution below
            // DO NOT write int main() function
            vector<TreeNode *> res;
            dfs(res, 1, n);
            return res;
        }
    };
  • 相关阅读:
    已解决: 已引发: "无法加载 DLL“opencv_core2410”: 找不到指定的模块。
    Xcode 设置图片全屏显示
    独创轻松实现拖拽,改变层布局
    WCF Odata 开放数据协议应用
    MVC中,加入的一个aspx页面用到AspNetPager控件处理办法
    关于 HRESULT:0x80070
    Springboot文件上传大小设置
    Jquery Validate 表单验证使用
    Quartz任务调度框架使用
    js中常见命令
  • 原文地址:https://www.cnblogs.com/graph/p/3254141.html
Copyright © 2011-2022 走看看