본문 바로가기
코딩 문제

[LeetCode] 2331. Evaluate Boolean Binary Tree, cpp

by Zach Choi 2023. 1. 10.
728x90
반응형

Solution

  • use recursive structure

Tag

  • Full binary tree
class Solution {
public:
	bool evaluateTree(TreeNode* root) {
		bool retval = true;

		if (root->left != nullptr && root->left != nullptr) // full binary tree
		{
			if (root->val == 2) // OR
			{
				retval = evaluateTree(root->left) || evaluateTree(root->right);;
			}
			else // operta = 3, AND
			{
				retval = evaluateTree(root->left) && evaluateTree(root->right);;
			}
		}
		else
		{
			retval = root->val;
		}

		return retval;
	}
};
728x90
반응형