Construct String from Binary Tree @LeetCode

PROBLEM :

You need to construct a string consists of parenthesis and integers from a binary tree with the preorder traversing way.

The null node needs to be represented by empty parenthesis pair "()". And you need to omit all the empty parenthesis pairs that don't affect the one-to-one mapping relationship between the string and the original binary tree.

Example 1:
Input: Binary tree: [1,2,3,4]
       1
     /   \
    2     3
   /  
  4    

Output: "1(2(4))(3)"

Explanation: Originallay it needs to be "1(2(4)())(3()())",
but you need to omit all the unnecessary empty parenthesis pairs.
And it will be "1(2(4))(3)".

Example 2:
Input: Binary tree: [1,2,3,null,4]
       1
     /   \
    2     3
     \
      4

Output: "1(2()(4))(3)"

Explanation: Almost the same as the first example,
except we can't omit the first parenthesis pair to break the one-to-one mapping relationship between the input and the output .

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */

class Solution {
public:
    string tree2str(TreeNode* t) {
        string str="" ;
       
        if(!t)
            return str ;
           
        TreeStr(t,&str) ;
        return str ;
    }
   
    void TreeStr(TreeNode *root,string *str){
       
        (*str)=(*str)+to_string(root->val);
       
        if(root->left){
            (*str)+='(' ;
            TreeStr(root->left,&(*str)) ;
            (*str)+=')' ;
        }
        else if(root->right){
            (*str)+="()" ;
        }
       
        if(root->right){
            (*str)+='(' ;
            TreeStr(root->right,&(*str)) ;
            (*str)+=')' ;
        }
    }
};

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */

class Solution {
public:
    string tree2str(TreeNode* root) {
        if(!root)
            return "" ;
           
        string str=to_string(root->val) ;
       
        if(root->left)
            str+="(" + tree2str(root->left) + ")" ;

        else if(root->right)
            str+="()" ;
           
        if(root->right)
            str+="(" + tree2str(root->right) + ")" ;
           
        return str ;
    }
};

--------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )