catch-up
This commit is contained in:
parent
70fa350da8
commit
14b51882bb
|
@ -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
|
||||
}
|
|
@ -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
|
||||
}
|
|
@ -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
|
||||
}
|
|
@ -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
|
||||
}
|
|
@ -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();
|
||||
*/
|
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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
|
||||
}
|
|
@ -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
|
||||
}
|
|
@ -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)
|
||||
}
|
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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
|
||||
}
|
|
@ -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
|
||||
}
|
|
@ -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]
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue