category-wise-problems

contains category wise problems(data structures, competitive) of popular platforms.

View the Project on GitHub mayankdutta/category-wise-problems

814. Binary Tree Pruning

for explanation refer to Delete leaves with Given Value

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    TreeNode dfs(TreeNode root) {
        if (root == null) {
            return root;
        }

        root.left  = dfs(root.left);
        root.right = dfs(root.right);

        if (root.val == 0) {
            if (root.left == null && root.right == null) {
                return null;
            }
        }
        return root;
    }

    public TreeNode pruneTree(TreeNode root) {
        root = dfs(root);
        return root;
    }
}