Insert into a Binary Search Tree

Insert into a Binary Search Tree

 

Explore - LeetCode

LeetCode Explore is the best place for everyone to start practicing and learning on LeetCode. No matter if you are a beginner or a master, there are always new topics waiting for you to explore.

leetcode.com

- 문제 내용 : BST에 value를 insert 하라

- 접근 방법

기본 내용이기 때문에 별 내용이 없다.

var insertIntoBST = function(root, val) {
    if(root == null) return new TreeNode(val);
    if(root.val < val) root.right = insertIntoBST(root.right, val);
    if(root.val > val) root.left = insertIntoBST(root.left, val);
    return root;
};
728x90
반응형

'Problem Solving' 카테고리의 다른 글

Two Sum III - Data structure design  (0) 2020.10.10
Serialize and Deserialize BST  (0) 2020.10.09
Rotate List  (0) 2020.10.07
Complement of Base 10 Integer  (0) 2020.10.05
Remove Covered Intervals  (0) 2020.10.04