buildTree.png

  1. 这个题得用后序遍历,用前序遍历删不干净
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public TreeNode pruneTree(TreeNode root) {

if (root == null) {
return null;
}

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

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

return root;

}