简单思路:遍历所有节点处理它们,如果该节点的左节点和右节点都为空且值为 0 则进行剪枝 (node = null)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {TreeNode}
*/
var pruneTree = function (root) {
return deal(root);
};
function deal(root) {
if (!root) return null;
if (root.left) {
root.left = deal(root.left);
}
if (root.right) {
root.right = deal(root.right);
}
if (!root.left && !root.right && root.val === 0) {
root = null;
}
return root;
}