1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-09-19 09:46:57 +02:00
LeetCode/cs/binary-tree-pruning.cs
Matej Focko 2351dfd0ee
chore: unwrap one layer
Signed-off-by: Matej Focko <mfocko@redhat.com>
2023-12-12 14:36:00 +01:00

36 lines
868 B
C#

/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class Solution {
private static bool PruneTreeRec(TreeNode? node) {
if (node == null) {
return false;
}
if (!PruneTreeRec(node.left)) {
node.left = null;
}
if (!PruneTreeRec(node.right)) {
node.right = null;
}
return node.val == 1 || node.left != null || node.right != null;
}
public TreeNode? PruneTree(TreeNode? root) {
if (!PruneTreeRec(root)) {
return null;
}
return root;
}
}