思路:递归
左右两个节点遍历完了才考虑中间节点,即后序遍历(左右中)
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def removeLeafNodes(self, root: TreeNode, target: int) -> TreeNode:
if not root:
return None
root.left=self.removeLeafNodes(root.left,target)
root.right=self.removeLeafNodes(root.right,target)
if not root.left and not root.right and root.val==target:
return None
return root