Skip to content

Commit 1c1a5eb

Browse files
authored
Update 701-insert-into-a-binary-search-tree.js
1 parent 8128f9f commit 1c1a5eb

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

701-insert-into-a-binary-search-tree.js

+22
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,25 @@ const insertIntoBST = function(root, val) {
4444
}
4545
return root;
4646
};
47+
48+
// another
49+
50+
/**
51+
* Definition for a binary tree node.
52+
* function TreeNode(val, left, right) {
53+
* this.val = (val===undefined ? 0 : val)
54+
* this.left = (left===undefined ? null : left)
55+
* this.right = (right===undefined ? null : right)
56+
* }
57+
*/
58+
/**
59+
* @param {TreeNode} root
60+
* @param {number} val
61+
* @return {TreeNode}
62+
*/
63+
const insertIntoBST = function(root, val) {
64+
if(root == null) return new TreeNode(val)
65+
if(val < root.val) root.left = insertIntoBST(root.left, val)
66+
else if(val > root.val) root.right = insertIntoBST(root.right, val)
67+
return root
68+
};

0 commit comments

Comments
 (0)