Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This can be done in O(n) .That is ,we visit each node of the tree only once. Logic is as follows Maintain two variables <strong>left</strong> and <strong>right</strong> and initialize them to zero. When ever there is recursive call to left side increment <strong>left</strong> by 1 When ever there is recursive call to ride side increment <strong>right</strong> by 1</p> <p>Starting from root,do inorder traversal and check whether <strong>right</strong> is zero, that means we never made recursive call to right. If yes print node, this means we are printing all leftmost nodes of tree .If <strong>right</strong> is non zero , they are not considered as boundaries,so look for leaf nodes and print them .</p> <p>In the Inorder traversal after left sub tree calls are done you bubble up to root and then we do recursive call for right sub tree. Now check for leaves nodes first and print them ,then check whether <strong>left</strong> is zero, that means we made recursive call to left ,so they are not considered as boundary. If <strong>left</strong> is zero print node, this means we are printing all rightmost nodes of tree .</p> <p>The code snippet is</p> <pre><code>void btree::cirn(struct node * root,int left,int right) { if(root == NULL) return; if(root) { if(right==0) { cout&lt;&lt;root-&gt;key_value&lt;&lt;endl; } cirn(root-&gt;left,left+1,right); if(root-&gt;left==NULL &amp;&amp; root-&gt;right==NULL &amp;&amp; right&gt;0) { cout&lt;&lt;root-&gt;key_value&lt;&lt;endl; } cirn(root-&gt;right,left,right+1); if(left==0) { if(right!=0) { cout&lt;&lt;root-&gt;key_value&lt;&lt;endl; } } } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload