#include <iostream>
#include <algorithm>
#include <string>
#define null 0

using namespace std;


class node {
private:
	char x;
	node* left;
	node* right;
public:
	node(char data = ' ', node* _left = null, node* _right = null) {
		if (data == '.') {
			data = null;
		}
		else {
			x = data;
		}
		left = _left;
		right = _right;
	}
	void print() {
		cout << this->x;
		return;
	}
	node* getleft() {
		return this->left;
	}
	node* getright() {
		return this->right;
	}
	void setleft(node* data) {
		this->left = data;
		return;
	}
	void setright(node* data) {
		this->right = data;
		return;
	}
	void preorder(node* current) {
		if (current == null) return;
		else {
			cout << current->x;
			preorder(current->getleft());
			preorder(current->getright());
		}
	}
	node* find(node* current, char data) {
		if (current == null) return null;
		else {
			if (current->x == data)	return current;
			else {
				if (find(current->left, data) == null) {
					find(current->right, data) == null;
				}
			}
		}
	}
};

int main() {
	node t = node();
	int N;
	cin >> N;
	for (int i = 0; i < N; i++) {
		char root, lc, rc;
		cin >> root >> lc >> rc;
		if (root == 'A') {
			node lt = node(lc);
			node rt = node(rc);
			t = node(root, &lt, &rt);
		}
		else {
			node* temp = new node;
			temp = t.find(&t, root);
			node templ = node(lc);
			temp->setleft(&templ);
			node tempr = node(rc);
			temp->setright(&tempr);
		}
	}
	t.preorder(&t);
	cout << endl;
	return 0;
}

이 코드가 오류가 난다.

백준 1991을 풀다가 발견한 에러인데

생성자에 조건문 같이 제어문을 사용하면 에러가 발생하는 것 같다

마지막에 setleft를 통해서 값을 설정하려고 할때 this를 지정하는 것이 temp값으로 고정되어야하는데

다른 값으로 튀는 경우가 있었다.

왜 인지는 구글링해도 모르겠다.

+ Recent posts