|
14 | 14 |
|
15 | 15 | <pre>
|
16 | 16 | <strong>输入:</strong> [1,null,0,0,1]
|
17 |
| -<strong>输出: </strong>[1,null,0,null,1] |
18 |
| -<strong>解释:</strong> |
| 17 | +<strong>输出: </strong>[1,null,0,null,1] |
| 18 | +<strong>解释:</strong> |
19 | 19 | 只有红色节点满足条件“所有不包含 1 的子树”。
|
20 | 20 | 右图为返回的答案。
|
21 | 21 |
|
|
27 | 27 | <pre>
|
28 | 28 | <strong>输入:</strong> [1,0,1,0,0,0,1]
|
29 | 29 | <strong>输出: </strong>[1,null,1,null,1]
|
30 |
| -<strong>解释:</strong> |
| 30 | +<strong>解释:</strong> |
31 | 31 |
|
32 | 32 | <img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/lcof2/%E5%89%91%E6%8C%87%20Offer%20II%20047.%20%E4%BA%8C%E5%8F%89%E6%A0%91%E5%89%AA%E6%9E%9D/images/1028_1.png" style="width:450px" />
|
33 | 33 | </pre>
|
|
37 | 37 | <pre>
|
38 | 38 | <strong>输入:</strong> [1,1,0,1,1,0,1,0]
|
39 | 39 | <strong>输出: </strong>[1,1,0,1,1,null,1]
|
40 |
| -<strong>解释:</strong> |
| 40 | +<strong>解释:</strong> |
41 | 41 |
|
42 | 42 | <img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/lcof2/%E5%89%91%E6%8C%87%20Offer%20II%20047.%20%E4%BA%8C%E5%8F%89%E6%A0%91%E5%89%AA%E6%9E%9D/images/1028.png" style="width:450px" />
|
43 | 43 | </pre>
|
|
67 | 67 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
68 | 68 |
|
69 | 69 | ```python
|
70 |
| - |
| 70 | +# Definition for a binary tree node. |
| 71 | +# class TreeNode: |
| 72 | +# def __init__(self, val=0, left=None, right=None): |
| 73 | +# self.val = val |
| 74 | +# self.left = left |
| 75 | +# self.right = right |
| 76 | +class Solution: |
| 77 | + def pruneTree(self, root: TreeNode) -> TreeNode: |
| 78 | + if not root: |
| 79 | + return None |
| 80 | + root.left = self.pruneTree(root.left) |
| 81 | + root.right = self.pruneTree(root.right) |
| 82 | + if root.val == 0 and not root.left and not root.right: |
| 83 | + return None |
| 84 | + return root |
71 | 85 | ```
|
72 | 86 |
|
73 | 87 | ### **Java**
|
74 | 88 |
|
75 | 89 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
76 | 90 |
|
77 | 91 | ```java
|
| 92 | +/** |
| 93 | + * Definition for a binary tree node. |
| 94 | + * public class TreeNode { |
| 95 | + * int val; |
| 96 | + * TreeNode left; |
| 97 | + * TreeNode right; |
| 98 | + * TreeNode() {} |
| 99 | + * TreeNode(int val) { this.val = val; } |
| 100 | + * TreeNode(int val, TreeNode left, TreeNode right) { |
| 101 | + * this.val = val; |
| 102 | + * this.left = left; |
| 103 | + * this.right = right; |
| 104 | + * } |
| 105 | + * } |
| 106 | + */ |
| 107 | +class Solution { |
| 108 | + public TreeNode pruneTree(TreeNode root) { |
| 109 | + if (root == null) { |
| 110 | + return null; |
| 111 | + } |
| 112 | + root.left = pruneTree(root.left); |
| 113 | + root.right = pruneTree(root.right); |
| 114 | + if (root.val == 0 && root.left == null && root.right == null) { |
| 115 | + return null; |
| 116 | + } |
| 117 | + return root; |
| 118 | + } |
| 119 | +} |
| 120 | +``` |
78 | 121 |
|
| 122 | +### **Go** |
| 123 | + |
| 124 | +```go |
| 125 | +/** |
| 126 | + * Definition for a binary tree node. |
| 127 | + * type TreeNode struct { |
| 128 | + * Val int |
| 129 | + * Left *TreeNode |
| 130 | + * Right *TreeNode |
| 131 | + * } |
| 132 | + */ |
| 133 | +func pruneTree(root *TreeNode) *TreeNode { |
| 134 | + if root == nil { |
| 135 | + return nil |
| 136 | + } |
| 137 | + root.Left = pruneTree(root.Left) |
| 138 | + root.Right = pruneTree(root.Right) |
| 139 | + if root.Val == 0 && root.Left == nil && root.Right == nil { |
| 140 | + return nil |
| 141 | + } |
| 142 | + return root |
| 143 | +} |
79 | 144 | ```
|
80 | 145 |
|
81 | 146 | ### **...**
|
|
0 commit comments