方法:后序遍历
搜索二叉树定义:左子树小于头节点,头节点小于右节点(在做题目时忘记了该定义)
/*** 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* trimBST(TreeNode* root, int low, int high) {if(root==NULL){return root;}if(root!=NULL){root->left=trimBST(root->left,low,high);root->right=trimBST(root->right,low,high);if(root->val>high){root=root->left;}else if(root->val<low){root=root->right;}}return root;}};
