remove half Nodes

remove half Nodes

·

1 min read

Remove all the half nodes and return the final binary tree.

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode(int val) {
        this.val = val;
        this.left = null;
        this.right = null;
    }
}

class Solution {
    public TreeNode removeHalfNodes(TreeNode root) {
        if (root == null)
            return null;

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

        if (root.left == null && root.right == null)
            return root;

        if (root.left == null)
            return root.right;

        if (root.right == null)
            return root.left;

        return root;
    }
}