Given the
Return the root of the trimmed binary search tree. Note that the root may change depending on the given bounds.
root
of a binary search tree and the lowest and highest boundaries as low
and high
, trim the tree so that all its elements lies in [low, high]
. Trimming the tree should not change the relative structure of the elements that will remain in the tree (i.e., any node's descendant should remain a descendant). It can be proven that there is a unique answer.Return the root of the trimmed binary search tree. Note that the root may change depending on the given bounds.
Example 1:
Input: root = [1,0,2], low = 1, high = 2
Output: [1,null,2]
Approach
Java
import java.util.ArrayList;import java.util.LinkedList;import java.util.List;import java.util.Queue;public class TrimBinarySearchTree {public static void main(String[] args) {TreeNode root = new TreeNode(1);root.left = new TreeNode(0);root.right = new TreeNode(2);int low = 1, high = 2;TreeNode tree = trimBST(root, low, high);printTree(tree);}static TreeNode trimBST(TreeNode root, int L, int R) {if (root == null)return root;// if root data is greater than R than cal for left subtreeif (root.val > R)return trimBST(root.left, L, R);// if root data is less than L than call for right subtreeif (root.val < L)return trimBST(root.right, L, R);// call for left subtreeroot.left = trimBST(root.left, L, R);// call for right subtreeroot.right = trimBST(root.right, L, R);return root;}static void printTree(TreeNode root) {Queue<TreeNode> q = new LinkedList<TreeNode>();q.add(root);List<String> vec = new ArrayList<String>();while (!q.isEmpty()) {root = q.poll();if (root == null)vec.add("null");elsevec.add(String.valueOf(root.val));if (root != null) {q.add(root.left);q.add(root.right);}}int j = vec.size() - 1;while (j > 0 && vec.get(j) == "null")j--;for (int i = 0; i < j; i++)System.out.print(vec.get(i) + ",");System.out.print(vec.get(j));}}class TreeNode {int val;TreeNode left;TreeNode right;TreeNode() {}TreeNode(int val) {this.val = val;}TreeNode(int val, TreeNode left, TreeNode right) {this.val = val;this.left = left;this.right = right;}}
C++
#include <bits/stdc++.h>using namespace std;//struct for treenodestruct TreeNode{int data;TreeNode *left;TreeNode *right;TreeNode(int data){this->data = data;this->left = NULL;this->right = NULL;}};TreeNode *trimBST(TreeNode *root, int L, int R){if (root == NULL)return root;//if root data is greater than R than cal for left subtreeif (root->data > R)return trimBST(root->left, L, R);//if root data is less than L than call for right subtreeif (root->data < L)return trimBST(root->right, L, R);//call for left subtreeroot->left = trimBST(root->left, L, R);//call for right subtreeroot->right = trimBST(root->right, L, R);return root;}//function to print the treevoid printTree(TreeNode *root){queue<TreeNode *> q;q.push(root);vector<string> vec;while (!q.empty()){root = q.front();q.pop();if (root == NULL)vec.push_back("null");elsevec.push_back(to_string(root->data));if (root != NULL){q.push(root->left);q.push(root->right);}}int j = vec.size() - 1;while (j > 0 && vec[j] == "null")j--;vec.resize(j);cout << "[";for (int i = 0; i < j; i++)cout << vec[i] << ",";cout << vec[j] << "]";}int main(){TreeNode *root = new TreeNode(1);root->left = new TreeNode(0);root->right = new TreeNode(2);int low = 1, high = 2;TreeNode *tree = trimBST(root, low, high);printTree(tree);return 0;}
No comments:
Post a Comment