solution 1:
//url:https://leetcode.com/problems/trim-a-binary-search-tree/description/
//这leetcode判断用时好像会有点问题,第一次提交,方案用时是在后30%,想了好一会有没有更优化的,发现好像没有,再次提交,就排到前90%了,有点怪
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* trimBST(TreeNode* root, int L, int R) {
if(!root)
return NULL;
if(root->valright,L,R);
if(root->val>R)
return trimBST(root->left,L,R);
root->left=trimBST(root->left,L,R);
root->right=trimBST(root->right,L,R);
return root;
}
};
- 本文作者: royalchen
- 本文链接: http://www.royalchen.com/2018/08/03/leetcode-669-trim-a-binary-search-tree/
- 版权声明: 本博客所有文章除特别声明外,均采用 MIT 许可协议。转载请注明出处!