All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0801_0900.s0814_binary_tree_pruning.Solution Maven / Gradle / Ivy

There is a newer version: 1.35
Show newest version
package g0801_0900.s0814_binary_tree_pruning;

// #Medium #Depth_First_Search #Tree #Binary_Tree
// #2022_03_23_Time_0_ms_(100.00%)_Space_39.9_MB_(75.94%)

import com_github_leetcode.TreeNode;

/*
 * 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;
 *     }
 * }
 */
public class Solution {
    public TreeNode pruneTree(TreeNode root) {
        if (root == null) {
            return root;
        }
        root.left = pruneTree(root.left);
        root.right = pruneTree(root.right);
        if (root.left == null && root.right == null && root.val == 0) {
            return null;
        }
        return root;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy