add py3/node soln

This commit is contained in:
2022-04-16 00:06:19 +05:30
parent 56e8fc22e6
commit 1153573284
3 changed files with 93 additions and 0 deletions

View File

@@ -0,0 +1,29 @@
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @param {number} low
* @param {number} high
* @return {TreeNode}
*/
function trimBST(root, low, high) {
if (!root) {
return null;
}
if (root.val < low) {
return trimBST(root.right, low, high);
} else if (root.val > high) {
return trimBST(root.left, low, high);
} else {
root.left = trimBST(root.left, low, high);
root.right = trimBST(root.right, low, high);
return root;
}
};