/**
 * struct TreeNode {
 *	int val;
 *	struct TreeNode *left;
 *	struct TreeNode *right;
 *	TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 * };
 */
#include <sstream>
#include <string>
class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param data string字符串 
     * @return TreeNode类
     */
    TreeNode* buildTree(string data) {
        // write code here
        if (data.empty()) return nullptr;

        istringstream iss(data);
        string item;
        getline(iss, item, ' ');
        if (item == "#") return nullptr;

        auto root = new TreeNode(stoi(item));
        queue<TreeNode*> nodeQueue;
        nodeQueue.push(root);

        while (!nodeQueue.empty()) {
            TreeNode* node = nodeQueue.front();
            nodeQueue.pop();

            if (!getline(iss, item, ' ')) break;
            if (item != "#") {
                node->left = new TreeNode(stoi(item));
                nodeQueue.push(node->left);
            }

            if (!getline(iss, item, ' ')) break;
            if (item != "#") {
                node->right = new TreeNode(stoi(item));
                nodeQueue.push(node->right);
            }
        }

        return root;
    }
};