#include <bits/stdc++.h>
using namespace std;
struct TreeNode{
	int val;
	vector<TreeNode*>children;
	TreeNode(int val):children({}),val(val){};
};
bool find(TreeNode* root,int d){
	if(root->val==d){
		return true;
	}
	
		for(auto &child:root->children){
	  if(child->val == d)return true;
	  //find(child,d);
	}
	for(auto &child:root->children){
	  find(child,d);
	}
	
	return false;
}
TreeNode* construct(vector<int>&arr){
	if(arr.empty()||arr[0]==-1)return nullptr;
	TreeNode* root = new TreeNode(arr[0]);
	stack<TreeNode*>st;
	st.push(root);
	
	for(int i = 1;i<arr.size();i++){
		auto u = st.top();
		if(arr[i]==-1){
			st.pop();
		}else{
			TreeNode* newNode = new TreeNode(arr[i]);
			u->children.push_back(newNode);
			st.push(newNode);
		}
	}
	return root;
}
int main() {
	vector<int>arr = {1,2,4,-1,5,-1,-1,3,6,-1,7,-1,-1,-1};
	TreeNode* root = construct(arr);
	int d = 4;
	bool ans = find(root,d);
	cout<<"val : "<<ans<<endl;
	return 0;
}