Swimmer

[LeetCode] 2331. Evaluate Boolean Binary Tree, cpp 본문

코딩 문제

[LeetCode] 2331. Evaluate Boolean Binary Tree, cpp

Zach Choi 2023. 1. 10. 19:57

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;
	}
};
Comments