add py3/node soln
This commit is contained in:
29
0669_trim-a-binary-search-tree/nodejs/solution.js
Normal file
29
0669_trim-a-binary-search-tree/nodejs/solution.js
Normal 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;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user