- Notifications
You must be signed in to change notification settings - Fork2.5k
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Uh oh!
There was an error while loading.Please reload this page.
Large diffs are not rendered by default.
Uh oh!
There was an error while loading.Please reload this page.
Large diffs are not rendered by default.
Uh oh!
There was an error while loading.Please reload this page.
Large diffs are not rendered by default.
Uh oh!
There was an error while loading.Please reload this page.
Large diffs are not rendered by default.
Uh oh!
There was an error while loading.Please reload this page.
Large diffs are not rendered by default.
Uh oh!
There was an error while loading.Please reload this page.
Large diffs are not rendered by default.
Uh oh!
There was an error while loading.Please reload this page.
| 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. |
Uh oh!
There was an error while loading.Please reload this page.