I have a new question, can you solve it?


  • 0
    Y

    The question mentioned "OJ's Binary Tree Serialization" :

    For example: "{1,2,3,#,#,4,#,#,5}"

    So when I was trying to solve this question, I got an idea!


    #Unique Binary Search Trees III
    It's similar to Unique Binary Search Trees II, but the return value should be a vector<string (or vector<int>)

    ##given n = 3
    ##your answer will be:

    • "1#3##2#" // {1,0,3,0,0,2,0}

    • "32#1###" // {3,2,0,1,0,0,0}

    • "31##2##" // {3,1,0,0,2,0,0}

    • "213####" // {2,1,3,0,0,0,0}

    • "1#2###3" // {1,0,2,0,0,0,3}

    #C++

    class Solution {
    public:
    	vector<vector<int> or string> generateTrees(int n) {
    
        }
    }
    

    ###If we create Tree first and then trans it into string. it's easy.
    ##But if we don't use struct TreeNode, how to solve it?
    ###One problem is: the return value will be quite long.
    ####if n = 10, string.length() will be 2^10


  • 0
    L

    for the problem " the return value will be quite long." it's really not an issue. seeing your example "1#2###3" // {1,0,2,0,0,0,3}, I think you don't know how to serialize a binary tree. It should be "1#2#3", #means termination of a path, so you don't need to write #'s children as new #.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.