Skip to content

Instantly share code, notes, and snippets.

@unilecs
Last active May 27, 2024 06:48
Show Gist options
  • Save unilecs/a81c8aeda472b7dabd4b323aee92d776 to your computer and use it in GitHub Desktop.
Save unilecs/a81c8aeda472b7dabd4b323aee92d776 to your computer and use it in GitHub Desktop.
Задача: Бинарное булево дерево
using System;
public class Program
{
public class TreeNode
{
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode(int val, TreeNode left=null, TreeNode right=null)
{
this.val = val;
this.left = left;
this.right = right;
}
}
public static bool EvaluateTree(TreeNode root) {
if (root.left == null && root.right == null) {
return root.val == 1;
}
bool left = EvaluateTree(root.left);
bool right = EvaluateTree(root.right);
return root.val == 2 ? left | right : left & right;
}
public static void Main()
{
Console.WriteLine("UniLecs");
// tests
var root1 = new TreeNode(2);
root1.left = new TreeNode(1);
root1.right = new TreeNode(3);
root1.right.left = new TreeNode(0);
root1.right.right = new TreeNode(1);
Console.WriteLine(EvaluateTree(root1).ToString()); // true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment