# 669. 修剪二叉搜索树

class Solution {
int low = 0;
int high = 0;

public TreeNode trimBST(TreeNode root, int low, int high) {
this.low = low;
this.high = high;
return inorder(root);
}

public TreeNode inorder(TreeNode root) {
if (root == null) {
return null;
}
if (root.val >= low && root.val <= high) {
root.left= inorder(root.left);
root.right = inorder(root.right);
} else if (root.val <= low) {
root.left = null;
root.right = inorder(root.right);
root = root.right;
} else {
root.right = null;
root.left = inorder(root.left);
root = root.left;
}
return root;
}
}


class Solution {
int low = 0;
int high = 0;

public TreeNode trimBST(TreeNode root, int low, int high) {
this.low = low;
this.high = high;
root = inorder(root);
return root;
}

public TreeNode inorder(TreeNode root) {
if (root == null) {
return null;
}
if (root.val < low) {
return inorder(root.right);
}
if (root.val > high) {
return inorder(root.left);
}
root.left = inorder(root.left);
root.right = inorder(root.right);
return root;
}
}

07-22
04-12 24

01-29 1144
07-31 126