s4553711
6/5/2017 - 2:33 PM

606.cpp

/**
 * 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) {
        return dfs(t);
    }
    string dfs(TreeNode* t) {
        if (t == NULL) return "";
        string ans = to_string(t->val);
        
        if (t->left != NULL) {
            ans += "("+dfs(t->left)+")";
        }
        
        if (t->left == NULL && t->right != NULL) {
            ans += "()";
        }
        
        if (t->right != NULL) {
            ans += "("+dfs(t->right)+")";
        }
        
        return ans;
    }
};