二叉树 
      
    
      
      
      
        树 
      
    
      
      
      
        深度优先搜索 
      
    
   
  
    
      
       
     
  
  
    
      
    
    
      
       
     
  
题目描述 
给你二叉树的根结点 root ,此外树的每个结点的值要么是 0 ,要么是 1 。
返回移除了所有不包含 1 的子树的原二叉树。
节点 node 的子树为 node 本身加上所有 node 的后代。
 
示例 1: 
输入: root = [1,null,0,0,1]
输出: [1,null,0,null,1]
解释: 
只有红色节点满足条件“所有不包含 1 的子树”。 右图为返回的答案。
 
示例 2: 
输入: root = [1,0,1,0,0,0,1]
输出: [1,null,1,null,1]
 
示例 3: 
输入: root = [1,1,0,1,1,0,1,0]
输出: [1,1,0,1,1,null,1]
 
 
提示: 
    树中节点的数目在范围 [1, 200] 内 
    Node.val 为 0 或 1 
 
解法 
方法一:递归 
我们首先判断当前节点是否为空,如果为空则直接返回空节点。
否则,我们递归地对左右子树进行剪枝,并将剪枝后的左右子树重新赋值给当前节点的左右子节点。然后判断当前节点的值是否为 0 且左右子节点都为空,如果是则返回空节点,否则返回当前节点。
时间复杂度 \(O(n)\) ,空间复杂度 \(O(n)\) 。其中 \(n\)  为二叉树的节点个数。
Python3 Java C++ Go TypeScript Rust JavaScript 
 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
10 
11 
12 
13 
14 
15 # 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   pruneTree ( self ,  root :  Optional [ TreeNode ])  ->  Optional [ TreeNode ]: 
        if  root  is  None : 
            return  root 
        root . left  =  self . pruneTree ( root . left ) 
        root . right  =  self . pruneTree ( root . right ) 
        if  root . val  ==  0  and  root . left  ==  root . right : 
            return  None 
        return  root 
 
 
 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 /** 
 * Definition for a binary tree node. 
 * public class TreeNode { 
 *     int val; 
 *     TreeNode left; 
 *     TreeNode right; 
 *     TreeNode() {} 
 *     TreeNode(int val) { this.val = val; } 
 *     TreeNode(int val, TreeNode left, TreeNode right) { 
 *         this.val = val; 
 *         this.left = left; 
 *         this.right = right; 
 *     } 
 * } 
 */ 
class  Solution   { 
     public   TreeNode   pruneTree ( TreeNode   root )   { 
         if   ( root   ==   null )   { 
             return   null ; 
         } 
         root . left   =   pruneTree ( root . left ); 
         root . right   =   pruneTree ( root . right ); 
         if   ( root . val   ==   0   &&   root . left   ==   null   &&   root . right   ==   null )   { 
             return   null ; 
         } 
         return   root ; 
     } 
} 
 
 
 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 /** 
 * Definition for a binary tree node. 
 * struct TreeNode { 
 *     int val; 
 *     TreeNode *left; 
 *     TreeNode *right; 
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {} 
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 
 * }; 
 */ 
class   Solution   { 
public : 
     TreeNode *   pruneTree ( TreeNode *   root )   { 
         if   ( ! root )   { 
             return   root ; 
         } 
         root -> left   =   pruneTree ( root -> left ); 
         root -> right   =   pruneTree ( root -> right ); 
         if   ( root -> val   ==   0   &&   root -> left   ==   root -> right )   { 
             return   nullptr ; 
         } 
         return   root ; 
     } 
}; 
 
 
 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 /** 
 * Definition for a binary tree node. 
 * type TreeNode struct { 
 *     Val int 
 *     Left *TreeNode 
 *     Right *TreeNode 
 * } 
 */ 
func   pruneTree ( root   * TreeNode )   * TreeNode   { 
     if   root   ==   nil   { 
         return   nil 
     } 
     root . Left   =   pruneTree ( root . Left ) 
     root . Right   =   pruneTree ( root . Right ) 
     if   root . Val   ==   0   &&   root . Left   ==   nil   &&   root . Right   ==   nil   { 
         return   nil 
     } 
     return   root 
} 
 
 
 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 /** 
 * Definition for a binary tree node. 
 * class TreeNode { 
 *     val: number 
 *     left: TreeNode | null 
 *     right: TreeNode | null 
 *     constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { 
 *         this.val = (val===undefined ? 0 : val) 
 *         this.left = (left===undefined ? null : left) 
 *         this.right = (right===undefined ? null : right) 
 *     } 
 * } 
 */ 
function   pruneTree ( root :   TreeNode   |   null ) :   TreeNode   |   null   { 
     if   ( ! root )   { 
         return   root ; 
     } 
     root . left   =   pruneTree ( root . left ); 
     root . right   =   pruneTree ( root . right ); 
     if   ( root . val   ===   0   &&   root . left   ===   root . right )   { 
         return   null ; 
     } 
     return   root ; 
} 
 
 
 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 
31 
32 
33 
34 
35 
36 
37 
38 // Definition for a binary tree node. 
// #[derive(Debug, PartialEq, Eq)] 
// pub struct TreeNode { 
//   pub val: i32, 
//   pub left: Option<Rc<RefCell<TreeNode>>>, 
//   pub right: Option<Rc<RefCell<TreeNode>>>, 
// } 
// 
// impl TreeNode { 
//   #[inline] 
//   pub fn new(val: i32) -> Self { 
//     TreeNode { 
//       val, 
//       left: None, 
//       right: None 
//     } 
//   } 
// } 
use   std :: cell :: RefCell ; 
use   std :: rc :: Rc ; 
impl   Solution   { 
     pub   fn   prune_tree ( root :   Option < Rc < RefCell < TreeNode >>> )   ->   Option < Rc < RefCell < TreeNode >>>   { 
         if   root . is_none ()   { 
             return   None ; 
         } 
         let   root   =   root . unwrap (); 
         let   left   =   Self :: prune_tree ( root . borrow_mut (). left . take ()); 
         let   right   =   Self :: prune_tree ( root . borrow_mut (). right . take ()); 
         if   root . borrow (). val   ==   0   &&   left . is_none ()   &&   right . is_none ()   { 
             return   None ; 
         } 
         root . borrow_mut (). left   =   left ; 
         root . borrow_mut (). right   =   right ; 
         Some ( root ) 
     } 
} 
 
 
 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 /** 
 * 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 )   { 
     if   ( ! root )   { 
         return   root ; 
     } 
     root . left   =   pruneTree ( root . left ); 
     root . right   =   pruneTree ( root . right ); 
     if   ( root . val   ===   0   &&   root . left   ===   root . right )   { 
         return   null ; 
     } 
     return   root ; 
};