From 14b51882bbf41658971dc03d7ee4927dd4559897 Mon Sep 17 00:00:00 2001 From: Gleb Koval Date: Thu, 9 Jun 2022 15:45:14 +0100 Subject: [PATCH] catch-up --- binary-tree-inorder-traversal/sol.go | 30 +++++++++++++ binary-tree-level-order-traversal/sol.go | 36 ++++++++++++++++ binary-tree-postorder-traversal/sol.go | 30 +++++++++++++ binary-tree-preorder-traversal/sol.go | 30 +++++++++++++ implement-queue-using-stacks/sol.rs | 54 ++++++++++++++++++++++++ insert-into-a-binary-search-tree/sol.go | 42 ++++++++++++++++++ invert-binary-tree/sol.go | 25 +++++++++++ maximum-depth-of-binary-tree/sol.go | 37 ++++++++++++++++ path-sum/sol.go | 25 +++++++++++ remove-palindromic-subsequences/sol.rs | 12 ++++++ search-in-a-binary-search-tree/sol.go | 29 +++++++++++++ symmetric-tree/sol.go | 38 +++++++++++++++++ two-sum-ii-input-array-is-sorted/sol.rs | 20 +++++++++ 13 files changed, 408 insertions(+) create mode 100644 binary-tree-inorder-traversal/sol.go create mode 100644 binary-tree-level-order-traversal/sol.go create mode 100644 binary-tree-postorder-traversal/sol.go create mode 100644 binary-tree-preorder-traversal/sol.go create mode 100644 implement-queue-using-stacks/sol.rs create mode 100644 insert-into-a-binary-search-tree/sol.go create mode 100644 invert-binary-tree/sol.go create mode 100644 maximum-depth-of-binary-tree/sol.go create mode 100644 path-sum/sol.go create mode 100644 remove-palindromic-subsequences/sol.rs create mode 100644 search-in-a-binary-search-tree/sol.go create mode 100644 symmetric-tree/sol.go create mode 100644 two-sum-ii-input-array-is-sorted/sol.rs diff --git a/binary-tree-inorder-traversal/sol.go b/binary-tree-inorder-traversal/sol.go new file mode 100644 index 0000000..03e11e3 --- /dev/null +++ b/binary-tree-inorder-traversal/sol.go @@ -0,0 +1,30 @@ +package main + +type TreeNode struct { + Val int + Left *TreeNode + Right *TreeNode +} + +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func inorderTraversalInPlace(root *TreeNode, list *[]int) { + if root == nil { + return + } + inorderTraversalInPlace(root.Left, list) + *list = append(*list, root.Val) + inorderTraversalInPlace(root.Right, list) +} + +func inorderTraversal(root *TreeNode) []int { + list := &[]int{} + inorderTraversalInPlace(root, list) + return *list +} diff --git a/binary-tree-level-order-traversal/sol.go b/binary-tree-level-order-traversal/sol.go new file mode 100644 index 0000000..ff9af8c --- /dev/null +++ b/binary-tree-level-order-traversal/sol.go @@ -0,0 +1,36 @@ +package main + +type TreeNode struct { + Val int + Left *TreeNode + Right *TreeNode +} + +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func levelOrder(root *TreeNode) [][]int { + list := [][]int{} + queue := []*TreeNode{root} + for { + current := []int{} + current_queue := queue + queue = queue[len(queue):] + for _, node := range current_queue { + if node != nil { + current = append(current, node.Val) + queue = append(queue, node.Left, node.Right) + } + } + if len(current) == 0 { + break + } + list = append(list, current) + } + return list +} diff --git a/binary-tree-postorder-traversal/sol.go b/binary-tree-postorder-traversal/sol.go new file mode 100644 index 0000000..613ac56 --- /dev/null +++ b/binary-tree-postorder-traversal/sol.go @@ -0,0 +1,30 @@ +package main + +type TreeNode struct { + Val int + Left *TreeNode + Right *TreeNode +} + +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func postorderTraversalInPlace(root *TreeNode, list *[]int) { + if root == nil { + return + } + postorderTraversalInPlace(root.Left, list) + postorderTraversalInPlace(root.Right, list) + *list = append(*list, root.Val) +} + +func postorderTraversal(root *TreeNode) []int { + list := &[]int{} + postorderTraversalInPlace(root, list) + return *list +} diff --git a/binary-tree-preorder-traversal/sol.go b/binary-tree-preorder-traversal/sol.go new file mode 100644 index 0000000..e9851fc --- /dev/null +++ b/binary-tree-preorder-traversal/sol.go @@ -0,0 +1,30 @@ +package main + +type TreeNode struct { + Val int + Left *TreeNode + Right *TreeNode +} + +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func preorderTraversalInPlace(root *TreeNode, list *[]int) { + if root == nil { + return + } + *list = append(*list, root.Val) + preorderTraversalInPlace(root.Left, list) + preorderTraversalInPlace(root.Right, list) +} + +func preorderTraversal(root *TreeNode) []int { + list := &[]int{} + preorderTraversalInPlace(root, list) + return *list +} diff --git a/implement-queue-using-stacks/sol.rs b/implement-queue-using-stacks/sol.rs new file mode 100644 index 0000000..638933c --- /dev/null +++ b/implement-queue-using-stacks/sol.rs @@ -0,0 +1,54 @@ +#[derive(Default)] +struct MyQueue { + front: Vec, + back: Vec +} + +// This solution has a maximum complexity of O(n) for +// all the operations combined, but a single operation +// could also be O(n) (apart from push and empty which +// is always O(1)) +/** + * `&self` means the method takes an immutable reference. + * If you need a mutable reference, change it to `&mut self` instead. + */ +impl MyQueue { + + fn new() -> Self { + Default::default() + } + + fn flush(&mut self) { + if self.front.is_empty() { + while let Some(n) = self.back.pop() { + self.front.push(n); + } + } + } + + fn push(&mut self, x: i32) { + self.back.push(x) + } + + fn pop(&mut self) -> i32 { + self.flush(); + self.front.pop().unwrap() // Unsafe unwrap because of instructions + } + + fn peek(&mut self) -> i32 { + self.flush(); + *self.front.last().unwrap() // Unsafe unwrap because of instructions + } + + fn empty(&self) -> bool { + self.front.is_empty() && self.back.is_empty() + } +} +/** + * Your MyQueue object will be instantiated and called as such: + * let obj = MyQueue::new(); + * obj.push(x); + * let ret_2: i32 = obj.pop(); + * let ret_3: i32 = obj.peek(); + * let ret_4: bool = obj.empty(); + */ \ No newline at end of file diff --git a/insert-into-a-binary-search-tree/sol.go b/insert-into-a-binary-search-tree/sol.go new file mode 100644 index 0000000..46c9c2c --- /dev/null +++ b/insert-into-a-binary-search-tree/sol.go @@ -0,0 +1,42 @@ +package main + +type TreeNode struct { + Val int + Left *TreeNode + Right *TreeNode +} + +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func insertIntoBST(root *TreeNode, val int) *TreeNode { + if root == nil { + return &TreeNode{ + Val: val, + } + } + node := &TreeNode{ + Val: val, + } + current := root + for { + if val < current.Val { + if current.Left == nil { + current.Left = node + return root + } + current = current.Left + } else { + if current.Right == nil { + current.Right = node + return root + } + current = current.Right + } + } +} diff --git a/invert-binary-tree/sol.go b/invert-binary-tree/sol.go new file mode 100644 index 0000000..ed55688 --- /dev/null +++ b/invert-binary-tree/sol.go @@ -0,0 +1,25 @@ +package main + +type TreeNode struct { + Val int + Left *TreeNode + Right *TreeNode +} + +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func invertTree(root *TreeNode) *TreeNode { + if root == nil { + return nil + } + root.Left, root.Right = root.Right, root.Left + invertTree(root.Left) + invertTree(root.Right) + return root +} diff --git a/maximum-depth-of-binary-tree/sol.go b/maximum-depth-of-binary-tree/sol.go new file mode 100644 index 0000000..9a5fece --- /dev/null +++ b/maximum-depth-of-binary-tree/sol.go @@ -0,0 +1,37 @@ +package main + +type TreeNode struct { + Val int + Left *TreeNode + Right *TreeNode +} + +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func maxDepth(root *TreeNode) int { + depth := 0 + queue := []*TreeNode{root} + if root == nil { + return 0 + } + for len(queue) != 0 { + current := queue + queue = queue[len(queue):] + for _, node := range current { + if node.Left != nil { + queue = append(queue, node.Left) + } + if node.Right != nil { + queue = append(queue, node.Right) + } + } + depth++ + } + return depth +} diff --git a/path-sum/sol.go b/path-sum/sol.go new file mode 100644 index 0000000..2c77c9f --- /dev/null +++ b/path-sum/sol.go @@ -0,0 +1,25 @@ +package main + +type TreeNode struct { + Val int + Left *TreeNode + Right *TreeNode +} + +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func hasPathSum(root *TreeNode, targetSum int) bool { + if root == nil { + return false + } + if root.Left == nil && root.Right == nil { + return targetSum == root.Val + } + return hasPathSum(root.Left, targetSum-root.Val) || hasPathSum(root.Right, targetSum-root.Val) +} diff --git a/remove-palindromic-subsequences/sol.rs b/remove-palindromic-subsequences/sol.rs new file mode 100644 index 0000000..389731a --- /dev/null +++ b/remove-palindromic-subsequences/sol.rs @@ -0,0 +1,12 @@ +impl Solution { + pub fn remove_palindrome_sub(s: String) -> i32 { + if s.bytes() + .rev() + .zip(s.bytes()) + .all(|(b1, b2)| b1 == b2) { + 1 + } else { + 2 + } + } +} \ No newline at end of file diff --git a/search-in-a-binary-search-tree/sol.go b/search-in-a-binary-search-tree/sol.go new file mode 100644 index 0000000..543c394 --- /dev/null +++ b/search-in-a-binary-search-tree/sol.go @@ -0,0 +1,29 @@ +package main + +type TreeNode struct { + Val int + Left *TreeNode + Right *TreeNode +} + +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func searchBST(root *TreeNode, val int) *TreeNode { + for root != nil { + if root.Val == val { + return root + } + if root.Val < val { + root = root.Right + } else { + root = root.Left + } + } + return nil +} diff --git a/symmetric-tree/sol.go b/symmetric-tree/sol.go new file mode 100644 index 0000000..371fe8f --- /dev/null +++ b/symmetric-tree/sol.go @@ -0,0 +1,38 @@ +package main + +type TreeNode struct { + Val int + Left *TreeNode + Right *TreeNode +} + +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func isSymmetric(root *TreeNode) bool { + if root == nil { + return true + } + queue := []*TreeNode{root} + for len(queue) != 0 { + l := len(queue) + current := queue + queue = queue[l:] + for idx, node := range current { + if (node == nil && current[l-idx-1] != nil) || + (node != nil && current[l-idx-1] == nil) || + (node != nil && node.Val != current[l-idx-1].Val) { + return false + } + if node != nil { + queue = append(queue, node.Left, node.Right) + } + } + } + return true +} diff --git a/two-sum-ii-input-array-is-sorted/sol.rs b/two-sum-ii-input-array-is-sorted/sol.rs new file mode 100644 index 0000000..4c35a13 --- /dev/null +++ b/two-sum-ii-input-array-is-sorted/sol.rs @@ -0,0 +1,20 @@ +impl Solution { + pub fn two_sum(numbers: Vec, target: i32) -> Vec { + let (mut front, mut back) = ( + numbers.iter(), + numbers.iter().rev() + ); + let (mut f_idx, mut f) = (1, *front.next().unwrap()); + let (mut b_idx, mut b) = (numbers.len() as i32, *back.next().unwrap()); + while f + b != target { + if f + b > target { + b = *back.next().unwrap(); + b_idx -= 1; + } else { + f = *front.next().unwrap(); + f_idx += 1; + } + } + vec![f_idx, b_idx] + } +} \ No newline at end of file