Skip to content

Commit

Permalink
Find whether list is in binary tree.
Browse files Browse the repository at this point in the history
  • Loading branch information
iglesias committed Sep 8, 2024
1 parent fc145ea commit 63a998c
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 0 deletions.
45 changes: 45 additions & 0 deletions leetcode/1367.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <memory>
#include <stdexcept>

struct list_node {
int val;
list_node *next;
list_node(int x) : val(x), next(nullptr) {}
list_node(int x, list_node *next) : val(x), next(next) {}
};

struct tree_node {
int val;
tree_node *left;
tree_node *right;
tree_node(int x) : val(x), left(nullptr), right(nullptr) {}
tree_node(int x, tree_node *left, tree_node *right) : val(x), left(left), right(right) {}
};

bool is_subpath_impl(list_node*, tree_node*);
bool is_subpath(list_node* head, tree_node* root) {
if (!root) return false;
return is_subpath_impl(head, root) or is_subpath(head, root->left) or is_subpath(head, root->right);
}
bool is_subpath_impl(list_node* listNode, tree_node* treeNode) {
if (!listNode) return true;
if (!treeNode) return false;
if (listNode->val != treeNode->val) return false;
return is_subpath_impl(listNode->next, treeNode->right) or is_subpath_impl(listNode->next, treeNode->left);
}

using std::unique_ptr;
using std::make_unique;

int main() {
unique_ptr<list_node> ln0 = make_unique<list_node>(1);
unique_ptr<list_node> ln1 = make_unique<list_node>(2, ln0.get());
unique_ptr<list_node> list = make_unique<list_node>(2, ln1.get());

unique_ptr<tree_node> leaf = make_unique<tree_node>(1);
unique_ptr<tree_node> l1 = make_unique<tree_node>(2, leaf.get(), nullptr);
unique_ptr<tree_node> l2 = make_unique<tree_node>(2, nullptr, l1.get());
unique_ptr<tree_node> tree = make_unique<tree_node>(2, l2.get(), nullptr);

if (!is_subpath(list.get(), tree.get())) throw std::domain_error("Test failed.");
}
84 changes: 84 additions & 0 deletions leetcode/1367.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ListNode {
pub val: i32,
pub next: Option<Box<ListNode>>
}

impl ListNode {
#[inline]
fn new(val: i32) -> Self {
ListNode {
next: None,
val
}
}
}

#[derive(Debug, Eq, PartialEq)]
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, left: Option<Rc<RefCell<TreeNode>>>, right: Option<Rc<RefCell<TreeNode>>>) -> Self {
TreeNode {
val,
left,
right
}
}
}

use std::rc::Rc;
use std::cell::RefCell;

pub fn is_sub_path(head: Option<Box<ListNode>>, root: Option<Rc<RefCell<TreeNode>>>) -> bool {
is_sub_path_internal(head.as_ref(), root.as_ref())
}

fn is_sub_path_internal(head: Option<&Box<ListNode>>, root: Option<&Rc<RefCell<TreeNode>>>) -> bool {
if root.is_none() {
return false;
}

let root_ref = root.unwrap().borrow();

is_sub_path_impl(head, root) ||
is_sub_path_internal(head, root_ref.left.as_ref()) ||
is_sub_path_internal(head, root_ref.right.as_ref())
}

fn is_sub_path_impl(list_node: Option<&Box<ListNode>>, tree_node: Option<&Rc<RefCell<TreeNode>>>) -> bool {
if list_node.is_none() {
return true;
}
if tree_node.is_none() {
return false;
}

let list_node = list_node.unwrap();
let tree_node_ref = tree_node.unwrap().borrow();

if list_node.val == tree_node_ref.val {
is_sub_path_impl(list_node.next.as_ref(), tree_node_ref.left.as_ref()) ||
is_sub_path_impl(list_node.next.as_ref(), tree_node_ref.right.as_ref())
} else {
false
}
}

fn main() {
let mut list = ListNode::new(2);
list.next = Some(Box::new(ListNode::new(2)));
list.next.as_mut().unwrap().next = Some(Box::new(ListNode::new(1)));

let leaf = TreeNode::new(1, None, None);
let l1 = TreeNode::new(2, Some(Rc::new(RefCell::new(leaf))), None);
let l2 = TreeNode::new(2, None, Some(Rc::new(RefCell::new(l1))));
let tree = TreeNode::new(2, Some(Rc::new(RefCell::new(l2))), None);

assert!(is_sub_path(Some(Box::new(list)), Some(Rc::new(RefCell::new(tree)))))
}

0 comments on commit 63a998c

Please sign in to comment.