701. 二叉搜索树中的插入操作
import { initTree, TreeNode } from './utils/ts-index';
function insertIntoBST(root: TreeNode | null, val: number): TreeNode | null {
if (!root) return new TreeNode(val);
if (val > root.val) {
// 进入右子树
root.right = insertIntoBST(root.right, val);
}
else if (val < root.val) {
// 进入左子树
root.left = insertIntoBST(root.left, val);
}
return root;
};
(()=> {
const root = initTree([4,2,7,1,3]);
console.log('insertIntoBST', insertIntoBST(root, 6));
})();