I solved this in 25 minutes on my own, no cheating.
Would anon hire me?
Thalidomide Vintage Ad Shirt $22.14 |
Nothing Ever Happens Shirt $21.68 |
Thalidomide Vintage Ad Shirt $22.14 |
I solved this in 25 minutes on my own, no cheating.
Would anon hire me?
Thalidomide Vintage Ad Shirt $22.14 |
Nothing Ever Happens Shirt $21.68 |
Thalidomide Vintage Ad Shirt $22.14 |
well, post your solution and let's have a look
lol frick off
>Would anon hire me?
No, but only because I know you use IQfy and are therefore very likely to be a bad programmer
I had to look this shit up.
Have you solved it?
Nah, someone on dpt posted this problem months ago. I tried doing it without looking it up struggled with it for an hour then gave up and looked up the answer.
Sorry i only hire hot chicks with big breasts.
Post solution then, Black person
I wrote out my C++ exam in fricking pencil I don't have to prove shit anymore.
ok uni tard, we'll see when real man applications hit you what the frick your pen and paper will be used for besides wiping your arse
This. Black folk made us write a sieve of eratosthenes program in C, using multiple processes, all on paper.
Uni teachers are fricking moronic.
Same, but in Java.
Same but in C
Now construct a non-binary tree you fricking bigot.
can you define preorder for a non-binary tree?
firstly you have a condition to make
if (rand()%100 <= 41){
int *p=NULL;
x=*p;
}
So a regular graph?
>doesn't specify return format
ok easy
tree(preorder,inorder){
return preorder
}
No, for the simple reason you're street shitter and used Java's build-in libraries, that or you copied it out of a book ( and used recursion ).
lel, frick off Ravinjavanamesh.
>not one solution posted yet
Is it really that hard?
floor.length.Math.console.log.random.then.addEventListener.function.log.Array.floor.console.Math.onclick.function.add.function.then.console.frick.jannies.add.console.button.log.addEventListener.add.Math.floor.log.then.onclick.random.log.button.length.floor.then.console.random.addEventListener.log.goodmorning.sirs.Math.then.floor.onclick.add.random.function.array.then.onclick.random.log.function.button.then.addEventListener.log.length.floor.needful.do.random.console.then.function.Math.log.console.length.random.Math.onclick.log.button.math.onclick.log.addEvenListener
>HELLO FRIENDS AND SALAM ALEIKUM TODAY I WILL SHOW YOU HOW TO CONSTRUCT BINARY TREE WITH THE PREORDER AND INORDER TRAVERSAL IN REACT JS
In Rust, this is just:
enum BinaryTree<T> {
Branch(T, Box<BinaryTree<T>>, Box<BinaryTree<T>>),
Leaf
}
impl<T: Eq> BinaryTree<T> {
fn from_traversals<Ts: IntoIterator<Item = T>>(preorder: Ts, inorder: &[T]) -> Self {
let mut preorder = preorder.into_iter().peekable();
Self::from_traversals_internal(&mut preorder, inorder)
}
fn from_traversals_internal<Ts: Iterator<Item = T>>(preorder: &mut Peekable<Ts>, inorder: &[T]) -> Self {
if let Some(i) = preorder.peek().and_then(|x| inorder.iter().position(|y| x == y)) {
let x = preorder.next().unwrap();
let left = Self::from_traversals_internal(preorder, &inorder[..i]);
let right = Self::from_traversals_internal(preorder, &inorder[i + 1..]);
Self::Branch(x, Box::new(left), Box::new(right))
} else {
Self::Leaf
}
}
}
The limits allow an n^2 solution, but can you do it faster?
You can accelerate the process of finding the index of each element in the in-order traversal by sorting or hashing it first, but that doesn't really change the solution.
25 minutes is not bad.
how do i learn to solve problems like this starting from zero if i have an iq of only 107 but am willing to work at it for a couple hours every day?
lol iq almost has nothing to do with it
repetition, just do them all day everyday and you'll get better at it. don't waste more than 25 minutes on a problem. If you get stuck, go look at the answer.. try to *fully understand* the code. what is it doing? why is it doing that?
then mark the problem and go to another one.. in a few days.. go back to that same problem and try to solve it.
you'll get better and better. You gotta try to game the system. Programming interviews are a different game then actually developing on stupid SCRUM AGILE teams or w/e at Microsoft.
Just gatekeeping shit. You'll get it anon.
Here's my thought process for this problem. There's a tree involved, so think about how you can divide the problem into subproblems whose solutions can be combined into a solution for the original problem. Looking at the example reveals this structure. It's then easy to devise a recursive algorithm.
NTA, what I don't understand is why you need the inorder array here?
The in-order traversal tells you, for each node, which elements are in the left subtree and which are in the right subtree.
the inorder array tells you where the third level (+) of items goes
Google only hires people who can do these in 15 minutes
In Haskell, this is just:
import Data.List (findIndex)
data BinaryTree a
= Branch a (BinaryTree a) (BinaryTree a)
| Leaf
fromTraversals :: Eq a => [a] -> [a] -> BinaryTree a
fromTraversals (x : preorder) inorder =
let Just n = findIndex (x ==) inorder in
let xl = fromTraversals (take n preorder) (take n inorder) in
let xr = fromTraversals (drop n preorder) (drop (n + 1) inorder) in
Branch x xl xr
fromTraversals [] [] = Leaf