How do I find a path for every leaf node in a tree? - depth-first-search

I am using the adjacency lists for storing the tree, I want a path for every leaf node from the root node.

Related

Fastest way to find one leaf node that is a child of a particular node in an unsorted tree

I'm implementing a Digital Search Tree's 'delete' function.
What tree traversal algorithms should I use to find a leaf node that is a child of the deleted node?
The leaf not doesn't have to be the closest one or anything. Just a leaf.
Thank you

Lowest Common Ancestor visiting every tree node only once

Can you say please, do we have a solution to find LCA of two nodes in binary tree visiting every tree node only once? Nodes only have left, right references without parent.
(O(n) time complexity).

What is Diameter of a Binary Tree? Is it Necessary that longest path for Diameter to go through the Root Node or not?

The diameter of a tree T is the largest of the following quantities:
the diameter of T’s left subtree
the diameter of T’s right subtree
the longest path between leaves that goes through the root of T (this can be computed from the heights of the subtrees of T).
I don't want the Code but just want to know what is right?
Please Check the Link
No, it is not necessary for diameter of a binary tree to pass through its root element (e.g: when the binary tree is unbalanced).
Lets get this:
Leaves of binary tree: nodes which have no chidren
Path: alternating set of nodes and edges which connect two paths.
In simpler terms (not exact definition) the diameter is longest path between any 2 leaf nodes in binary tree.
Inferred from: http://www.geeksforgeeks.org/diameter-of-a-binary-tree/

What is node in a tree?

According to wiki, everything in a tree is a node.
Terminologies used in Trees
Root – The top node in a tree.
Parent – The converse notion of child.
Siblings – Nodes with the same parent.
Descendant – a node reachable by repeated proceeding from parent to child.
Ancestor – a node reachable by repeated proceeding from child to parent.
Leaf – a node with no children.
Internal node – a node with at least one child.
External node – a node with no children.
Degree – number of sub trees of a node.
Edge – connection between one node to another.
Path – a sequence of nodes and edges connecting a node with a descendant.
Level – The level of a node is defined by 1 + the number of connections between the node and the root.
Height of tree –The height of a tree is the number of edges on the longest downward path between the root and a leaf.
Height of node –The height of a node is the number of edges on the longest downward path between that node and a leaf.
Depth –The depth of a node is the number of edges from the node to the tree's root node.
Forest – A forest is a set of n ≥ 0 disjoint trees.
But then I find the following picture from SAP http://www.sapdesignguild.org/community/design/print_hierarchies2.asp
So my question - is it right to call root, leaf, parents, children, siblings in a tree as nodes?
Yes. The root is "the root node." A parent is a "parent node." A leaf is a "leaf node." The tree is made up of nodes. The terms root, parent, child, sibling, leaf, etc. just describe the relationships among nodes.
For example, the root node has no parent. Leaf nodes have no children. Sibling nodes share the same parent.

iterative approach for tree traversal

Can someone help me out with an algorithm to traverse a binary tree iteratively without using any other data structure like a stack
I read somewhere we can have a flag named visited for each node and turn in on if the node is visited but my BinaryTreeNode class does not have a visited variable defined. So I can not potentially do something like node.left.visited = false
Is there any other way to traverse iteratively?
One option would be to thread the binary tree.
Whenever some node points to NULL (be it left or right), make that node point to the node which comes next in its traversal (pre-order, post-order, etc). In this way, you can traverse the entire tree in one iteration.
Sample threaded binary tree:
Note that left node of each node points to the largest value smaller than it. And the right node of each node points to the smallest value larger than it. So this gives an in-order traversal.

Resources