mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-14 01:49:41 +01:00
problems(cs): add „814. Binary Tree Pruning“
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
23a0bb299a
commit
20e3625bd7
1 changed files with 36 additions and 0 deletions
36
problems/binary-tree-pruning.cs
Normal file
36
problems/binary-tree-pruning.cs
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
/**
|
||||||
|
* 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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue