非常简单的一道题,题号814
整体上来说就是递归的思想,先从最底层开始剪枝,当所有子叶剪完再剪上一层,所有我们需要用后续遍历
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode pruneTree(TreeNode root) {
if(root == null) return null;
pruneTree(root.left);
pruneTree(root.right);
if(root.val == 0 && (root.left == null && root.right == null)){
return null;
}
return root;
}
}