This commit is contained in:
Gleb Koval 2022-06-09 15:45:14 +01:00
parent 70fa350da8
commit 14b51882bb
Signed by: cyclane
GPG Key ID: 15E168A8B332382C
13 changed files with 408 additions and 0 deletions

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -0,0 +1,54 @@
#[derive(Default)]
struct MyQueue {
front: Vec<i32>,
back: Vec<i32>
}
// 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();
*/

View File

@ -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
}
}
}

25
invert-binary-tree/sol.go Normal file
View File

@ -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
}

View File

@ -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
}

25
path-sum/sol.go Normal file
View File

@ -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)
}

View File

@ -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
}
}
}

View File

@ -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
}

38
symmetric-tree/sol.go Normal file
View File

@ -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
}

View File

@ -0,0 +1,20 @@
impl Solution {
pub fn two_sum(numbers: Vec<i32>, target: i32) -> Vec<i32> {
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]
}
}