Last active
May 27, 2024 06:48
-
-
Save unilecs/a81c8aeda472b7dabd4b323aee92d776 to your computer and use it in GitHub Desktop.
Задача: Бинарное булево дерево
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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