oppgaver så langt

This commit is contained in:
Håkon Størdal 2025-03-22 13:49:25 +01:00
parent 4f3385850f
commit df84d4f18e
17 changed files with 628 additions and 0 deletions

6
same-tree-100/Cargo.toml Normal file
View file

@ -0,0 +1,6 @@
[package]
name = "same-tree-100"
version = "0.1.0"
edition = "2024"
[dependencies]

181
same-tree-100/src/main.rs Normal file
View file

@ -0,0 +1,181 @@
// Definition for a binary tree node.
#[derive(Debug, PartialEq, Eq)]
pub struct TreeNode {
pub val: i32,
pub left: Option<Rc<RefCell<TreeNode>>>,
pub right: Option<Rc<RefCell<TreeNode>>>,
}
impl TreeNode {
#[inline]
pub fn new(val: i32) -> Self {
TreeNode {
val,
left: None,
right: None,
}
}
}
struct Solution {}
use std::cell::RefCell;
use std::rc::Rc;
impl Solution {
pub fn is_same_tree(
p: Option<Rc<RefCell<TreeNode>>>,
q: Option<Rc<RefCell<TreeNode>>>,
) -> bool {
// Base cases: if either is None
match (p, q) {
// Both are None - trees are identical (empty)
(None, None) => true,
// One is None but the other isn't - trees are different
(Some(_), None) | (None, Some(_)) => false,
// Both trees have nodes - compare them recursively
(Some(p_node), Some(q_node)) => {
let p_borrow = p_node.borrow();
let q_borrow = q_node.borrow();
// Trees are the same if:
// 1. Current nodes have the same value
// 2. Left subtrees are the same
// 3. Right subtrees are the same
p_borrow.val == q_borrow.val
&& Solution::is_same_tree(p_borrow.left.clone(), q_borrow.left.clone())
&& Solution::is_same_tree(p_borrow.right.clone(), q_borrow.right.clone())
}
}
}
}
pub fn is_same_tree_iterativly(p: Option<Rc<RefCell<TreeNode>>>, q: Option<Rc<RefCell<TreeNode>>>) -> bool {
// Create a stack to store pairs of nodes to compare
let mut stack = Vec::new();
// Push the root pair to the stack
stack.push((p, q));
// Process nodes until the stack is empty
while !stack.is_empty() {
if let Some((p_opt, q_opt)) = stack.pop() {
match (p_opt, q_opt) {
// Both nodes are None, continue checking other pairs
(None, None) => continue,
// One node is None while the other isn't - trees differ
(Some(_), None) | (None, Some(_)) => return false,
// Both nodes exist, compare them
(Some(p_node), Some(q_node)) => {
let p_borrow = p_node.borrow();
let q_borrow = q_node.borrow();
// If values are different, trees differ
if p_borrow.val != q_borrow.val {
return false;
}
// Push right children to stack
stack.push((
p_borrow.right.clone(),
q_borrow.right.clone()
));
// Push left children to stack
stack.push((
p_borrow.left.clone(),
q_borrow.left.clone()
));
}
}
}
}
// If we've processed all nodes without finding differences, trees are identical
true
}
// Helper function to create trees from arrays
fn create_tree_from_vec(nums: &[Option<i32>], index: usize) -> Option<Rc<RefCell<TreeNode>>> {
if index >= nums.len() || nums[index].is_none() {
return None;
}
let val = nums[index].unwrap();
let mut node = TreeNode::new(val);
// Create left child (2*index + 1)
node.left = create_tree_from_vec(nums, 2 * index + 1);
// Create right child (2*index + 2)
node.right = create_tree_from_vec(nums, 2 * index + 2);
Some(Rc::new(RefCell::new(node)))
}
fn main() {
// Test case 1: Two identical trees
// 1
// / \
// 2 3
let tree1_data = vec![Some(1), Some(2), Some(3)];
let tree1 = create_tree_from_vec(&tree1_data, 0);
let tree2 = create_tree_from_vec(&tree1_data, 0);
// Expected: true
println!("Test case 1: {}", is_same_tree_iterativly(tree1, tree2));
// Test case 2: Different values in nodes
// 1 1
// / \ / \
// 2 3 2 4
let tree3_data = vec![Some(1), Some(2), Some(3)];
let tree4_data = vec![Some(1), Some(2), Some(4)];
let tree3 = create_tree_from_vec(&tree3_data, 0);
let tree4 = create_tree_from_vec(&tree4_data, 0);
// Expected: false
println!("Test case 2: {}", Solution::is_same_tree(tree3, tree4));
// Test case 3: Different structure
// 1 1
// / \ \
// 2 3 3
let tree5_data = vec![Some(1), Some(2), Some(3)];
let tree6_data = vec![Some(1), None, Some(3)];
let tree5 = create_tree_from_vec(&tree5_data, 0);
let tree6 = create_tree_from_vec(&tree6_data, 0);
// Expected: false
println!("Test case 3: {}", Solution::is_same_tree(tree5, tree6));
// Test case 4: Empty trees
let tree7: Option<Rc<RefCell<TreeNode>>> = None;
let tree8: Option<Rc<RefCell<TreeNode>>> = None;
// Expected: true
println!("Test case 4: {}", Solution::is_same_tree(tree7, tree8));
// Test case 5: One empty, one not
let tree9_data = vec![Some(1)];
let tree9 = create_tree_from_vec(&tree9_data, 0);
let tree10: Option<Rc<RefCell<TreeNode>>> = None;
// Expected: false
println!("Test case 5: {}", Solution::is_same_tree(tree9, tree10));
// Test case 6: More complex identical trees
// 1
// / \
// 2 3
// / \
// 4 5
let tree11_data = vec![Some(1), Some(2), Some(3), Some(4), None, None, Some(5)];
let tree11 = create_tree_from_vec(&tree11_data, 0);
let tree12 = create_tree_from_vec(&tree11_data, 0);
// Expected: true
println!("Test case 6: {}", Solution::is_same_tree(tree11, tree12));
}