Skip to content

Sri Hari: Batch-5/Neetcode-250/Added-articles #3791

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
467 changes: 467 additions & 0 deletions articles/binary-tree-inorder-traversal.md

Large diffs are not rendered by default.

662 changes: 662 additions & 0 deletions articles/binary-tree-postorder-traversal.md

Large diffs are not rendered by default.

468 changes: 468 additions & 0 deletions articles/binary-tree-preorder-traversal.md

Large diffs are not rendered by default.

733 changes: 733 additions & 0 deletions articles/construct-quad-tree.md

Large diffs are not rendered by default.

577 changes: 577 additions & 0 deletions articles/delete-leaves-with-a-given-value.md

Large diffs are not rendered by default.

632 changes: 632 additions & 0 deletions articles/delete-node-in-a-bst.md

Large diffs are not rendered by default.

457 changes: 457 additions & 0 deletions articles/house-robber-iii.md

Large diffs are not rendered by default.

292 changes: 292 additions & 0 deletions articles/insert-into-a-binary-search-tree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,292 @@
## 1. Recursion

::tabs-start

```python
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def insertIntoBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
if not root:
return TreeNode(val)

if val > root.val:
root.right = self.insertIntoBST(root.right, val)
else:
root.left = self.insertIntoBST(root.left, val)

return root
```

```java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class Solution {
public TreeNode insertIntoBST(TreeNode root, int val) {
if (root == null) {
return new TreeNode(val);
}

if (val > root.val) {
root.right = insertIntoBST(root.right, val);
} else {
root.left = insertIntoBST(root.left, val);
}

return root;
}
}
```

```cpp
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
TreeNode* insertIntoBST(TreeNode* root, int val) {
if (!root) {
return new TreeNode(val);
}

if (val > root->val) {
root->right = insertIntoBST(root->right, val);
} else {
root->left = insertIntoBST(root->left, val);
}

return root;
}
};
```

```javascript
/**
* Definition for a binary tree node.
* class TreeNode {
* constructor(val = 0, left = null, right = null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
/**
* @param {TreeNode} root
* @param {number} val
* @return {TreeNode}
*/
insertIntoBST(root, val) {
if (!root) {
return new TreeNode(val);
}

if (val > root.val) {
root.right = this.insertIntoBST(root.right, val);
} else {
root.left = this.insertIntoBST(root.left, val);
}

return root;
}
}
```

::tabs-end

### Time & Space Complexity

* Time complexity: $O(h)$
* Space complexity: $O(h)$ for the recursion stack.

> Where $h$ is the height of the given binary search tree.

---

## 2. Iteration

::tabs-start

```python
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def insertIntoBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
if not root:
return TreeNode(val)

cur = root
while True:
if val > cur.val:
if not cur.right:
cur.right = TreeNode(val)
return root
cur = cur.right
else:
if not cur.left:
cur.left = TreeNode(val)
return root
cur = cur.left
```

```java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class Solution {
public TreeNode insertIntoBST(TreeNode root, int val) {
if (root == null) {
return new TreeNode(val);
}

TreeNode cur = root;
while (true) {
if (val > cur.val) {
if (cur.right == null) {
cur.right = new TreeNode(val);
return root;
}
cur = cur.right;
} else {
if (cur.left == null) {
cur.left = new TreeNode(val);
return root;
}
cur = cur.left;
}
}
}
}
```

```cpp
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
TreeNode* insertIntoBST(TreeNode* root, int val) {
if (!root) {
return new TreeNode(val);
}

TreeNode* cur = root;
while (true) {
if (val > cur->val) {
if (!cur->right) {
cur->right = new TreeNode(val);
return root;
}
cur = cur->right;
} else {
if (!cur->left) {
cur->left = new TreeNode(val);
return root;
}
cur = cur->left;
}
}
}
};
```

```javascript
/**
* Definition for a binary tree node.
* class TreeNode {
* constructor(val = 0, left = null, right = null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
/**
* @param {TreeNode} root
* @param {number} val
* @return {TreeNode}
*/
insertIntoBST(root, val) {
if (!root) {
return new TreeNode(val);
}

let cur = root;
while (true) {
if (val > cur.val) {
if (!cur.right) {
cur.right = new TreeNode(val);
return root;
}
cur = cur.right;
} else {
if (!cur.left) {
cur.left = new TreeNode(val);
return root;
}
cur = cur.left;
}
}
}
}
```

::tabs-end

### Time & Space Complexity

* Time complexity: $O(h)$
* Space complexity: $O(1)$ extra space.

> Where $h$ is the height of the given binary search tree.
Loading