|
70 | 70 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
71 | 71 |
|
72 | 72 | ```python
|
73 |
| - |
| 73 | +# Definition for a binary tree node. |
| 74 | +# class TreeNode: |
| 75 | +# def __init__(self, val=0, left=None, right=None): |
| 76 | +# self.val = val |
| 77 | +# self.left = left |
| 78 | +# self.right = right |
| 79 | +class Solution: |
| 80 | + def trimBST(self, root: Optional[TreeNode], low: int, high: int) -> Optional[TreeNode]: |
| 81 | + def dfs(root): |
| 82 | + if root is None: |
| 83 | + return root |
| 84 | + if root.val > high: |
| 85 | + return dfs(root.left) |
| 86 | + if root.val < low: |
| 87 | + return dfs(root.right) |
| 88 | + root.left = dfs(root.left) |
| 89 | + root.right = dfs(root.right) |
| 90 | + return root |
| 91 | + |
| 92 | + return dfs(root) |
74 | 93 | ```
|
75 | 94 |
|
76 | 95 | ### **Java**
|
77 | 96 |
|
78 | 97 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
79 | 98 |
|
80 | 99 | ```java
|
| 100 | +/** |
| 101 | + * Definition for a binary tree node. |
| 102 | + * public class TreeNode { |
| 103 | + * int val; |
| 104 | + * TreeNode left; |
| 105 | + * TreeNode right; |
| 106 | + * TreeNode() {} |
| 107 | + * TreeNode(int val) { this.val = val; } |
| 108 | + * TreeNode(int val, TreeNode left, TreeNode right) { |
| 109 | + * this.val = val; |
| 110 | + * this.left = left; |
| 111 | + * this.right = right; |
| 112 | + * } |
| 113 | + * } |
| 114 | + */ |
| 115 | +class Solution { |
| 116 | + public TreeNode trimBST(TreeNode root, int low, int high) { |
| 117 | + if (root == null) { |
| 118 | + return root; |
| 119 | + } |
| 120 | + if (root.val > high) { |
| 121 | + return trimBST(root.left, low, high); |
| 122 | + } |
| 123 | + if (root.val < low) { |
| 124 | + return trimBST(root.right, low, high); |
| 125 | + } |
| 126 | + root.left = trimBST(root.left, low, high); |
| 127 | + root.right = trimBST(root.right, low, high); |
| 128 | + return root; |
| 129 | + } |
| 130 | +} |
| 131 | +``` |
| 132 | + |
| 133 | +### **C++** |
| 134 | + |
| 135 | +```cpp |
| 136 | +/** |
| 137 | + * Definition for a binary tree node. |
| 138 | + * struct TreeNode { |
| 139 | + * int val; |
| 140 | + * TreeNode *left; |
| 141 | + * TreeNode *right; |
| 142 | + * TreeNode() : val(0), left(nullptr), right(nullptr) {} |
| 143 | + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} |
| 144 | + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} |
| 145 | + * }; |
| 146 | + */ |
| 147 | +class Solution { |
| 148 | +public: |
| 149 | + TreeNode* trimBST(TreeNode* root, int low, int high) { |
| 150 | + if (!root) return root; |
| 151 | + if (root->val > high) return trimBST(root->left, low, high); |
| 152 | + if (root->val < low) return trimBST(root->right, low, high); |
| 153 | + root->left = trimBST(root->left, low, high); |
| 154 | + root->right = trimBST(root->right, low, high); |
| 155 | + return root; |
| 156 | + } |
| 157 | +}; |
| 158 | +``` |
81 | 159 |
|
| 160 | +### **Go** |
| 161 | +
|
| 162 | +```go |
| 163 | +/** |
| 164 | + * Definition for a binary tree node. |
| 165 | + * type TreeNode struct { |
| 166 | + * Val int |
| 167 | + * Left *TreeNode |
| 168 | + * Right *TreeNode |
| 169 | + * } |
| 170 | + */ |
| 171 | +func trimBST(root *TreeNode, low int, high int) *TreeNode { |
| 172 | + if root == nil { |
| 173 | + return root |
| 174 | + } |
| 175 | + if root.Val > high { |
| 176 | + return trimBST(root.Left, low, high) |
| 177 | + } |
| 178 | + if root.Val < low { |
| 179 | + return trimBST(root.Right, low, high) |
| 180 | + } |
| 181 | + root.Left = trimBST(root.Left, low, high) |
| 182 | + root.Right = trimBST(root.Right, low, high) |
| 183 | + return root |
| 184 | +} |
82 | 185 | ```
|
83 | 186 |
|
84 | 187 | ### **...**
|
|
0 commit comments