What is the lowest common ancestor of 5 and 4 in this binary tree - binary-tree

Binary tree
What is the lowest common ancestor of 5 and 4 in the binary tree above, considering we allow a node to be a descendant of itself. Wouldn't it be 3? if not, what would it be and why?

As we allow a node to be a descendant of itself, LCA will be 5.

Related

Does each level in a binary search tree need to be ordered?

I'm reading about binary search trees, and most resources state that binary search trees need to abide by two rules (vs. regular trees):
Each node has a maximum of up to two children.
For each node, the values of its left descendent nodes are less than that of the current node, which in turn is less than the right descendent nodes (if any).
But does that mean we can have a tree where the levels are not in chronological order? For example, I found this image online and wasn't sure if it was a valid binary search tree since the third level from the top is not in order. So if I wanted to search for the number 4 I wouldn't know whether to search for it in the left or right branch.
This is not a binary search tree, as descendent nodes of a node u include all nodes that can be accessed going down a path starting at u.
Here 11 -> 12 -> 4 is a path down from 11, going first through the right child 12, so 4 is a right descendent node of 11. Since its value is smaller, this tree is not a binary search tree.
As a consequence of these rules, all layers are indeed sorted. In fact the whole in-order traversal is sorted.

How to make Full Binary Tree with 6 nodes?

I know well about Full Binary Tree and Complete Binary Tree. But unable to make Full binary tree with only 6 nodes.
The answer is No. You can't make a Full binary tree with just 6 nodes. As the definition in the Wikipedia says:
A full binary tree (sometimes referred to as a proper or plane
binary tree) is a tree in which every node has either 0 or 2
children. Another way of defining a full binary tree is a recursive
definition. A full binary tree is either:
A single vertex.
A tree whose root node has two subtrees, both of which are full binary trees.
Another interesting property I noticed is that, the number of nodes required to make a full binary tree will always be odd.
Another way to see that a full binary tree has an odd number of nodes:
Starting with the definition of a full binary tree (Wikipedia):
a tree in which every node has either 0 or 2 children.
This means that the total number of child nodes is even (0+2+2+0+...+2 is always even). There is only one node that is not a child of another, which is the root. So considering that node as well, the total becomes odd.
By consequence there is no full binary tree with 6 nodes.
Elaborating on #vivek_23's answer, this is, unfortunately, not possible. There's a beautiful theorem that says the following:
Theorem: Any full binary tree has 2L - 1 nodes, where L is the number of leaf nodes in the tree.
The intuition behind this theorem is actually pretty simple. Imagine you take a complete binary tree and delete all the internal nodes from it. You now have a forest of L single-node full binary trees, one for each leaf. Now, add the internal nodes back one at a time. Each time you do, you'll be taking two different trees in the forest and combining them into a single tree, which decreases the number of trees in the forest by one. This means that you have to have exactly L - 1 internal nodes, since if you had any fewer you wouldn't be able to join together all the trees in the forest, and if you had any more you'd run out of trees to combine.
The fact that there are 2L - 1 total nodes in a full binary tree means that the number of nodes in a full binary tree is always odd, so you can't create a full binary tree with 6 nodes. However, you can create a full binary tree with any number of odd nodes - can you figure out how to prove that?
Hope this helps!

How to build binary tree knowing only which nodes are connected?

I have to build a binary tree but i don't know which node is parent, left child, or right child. I only know which nodes are connected. Example: for input like this:
6 4
5 7
9 7
1 5
10 4
3 4
2 6
7 8
5 6
(from 1 there is always one path) the tree should looks like that:
One the input i have also given number of nodes. Any ideas, tips?
From the list of the edges, one can easily create a tree.
You can find which are the leaf nodes, just search which nodes are appearing only in one edge.
But, it is not possible to know which of the leaf nodes is the head of the tree.
In a tree structure it is possible to choose any leaf, choose it as the head, and reorder the tree, and it'll be a valid tree.
There is also the issue of tree isomorphism, you can swap the right and left sub-trees to get a valid tree.
To summarize, from this list you can get 6 heads and in each 4 possible swaps, so in total 24 different valid trees.

Find the maximum weight node in a tree if each node is the sum of the weights all the nodes under it.

For exa, this is the tree.
10
12 -1
5 1 1 -2
2 3 10 -9
How to find the node with maximum value?
Given the problem as stated, you need to traverse the entire tree. See proof below.
Traversing the entire tree should be a fairly trivial process.
Proof that we need to traverse the entire tree:
Assume we're able to identify which side of a tree the maximum is on without traversing the entire tree.
Given any tree with the maximum node on the left. Call this maximum x.
Pick one of the leaf nodes on the right. Add 2 children to it: x+1 and -x-1.
Since x+1-x-1 = 0, adding these won't change the sum at the leaf we added it to, thus nor the sums at any other nodes in the tree.
Since this can be added to any leaf in the tree, and it doesn't affect the sums, we'd need to traverse the entire tree to find out if this occurs anywhere.
Thus our assumption that we can identify which side of a tree the maximum is on without traversing the entire tree is incorrect.
Thus we need to traverse the entire tree.
In the general case, you need to traverse the entire tree. If the values in the tree are not constrained (e.g. all non-negative, but in your example there are negative values), then the value in a node tells you nothing about the individual values below it.

First Common Ancestor of a Binary Tree

If I have a binary search tree like this then what will be lowest common ancestor of nodes 6 and 1?
According to the Wikipedia definition of the Lowest common ancestor I correct myself:
The lowest common ancestor (LCA) is a concept in graph theory and
computer science. Let T be a rooted tree with n nodes. The lowest
common ancestor is defined between two nodes v and w as the lowest
node in T that has both v and w as descendants (where we allow a node
to be a descendant of itself).
So yes going by this definition the correct answer would be 6. If this is an interview question would be good to clarify in advance with the interviewer.

Resources