Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8604271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:31:53+00:00 2026-06-12T02:31:53+00:00

1 / \ 2 3 / \ / \ 4 5 6 7 for

  • 0
     1
   /  \
  2    3
 / \   / \
4   5 6   7

for the given binary tree we need to create a matrix a[7][7]
satisfying the ancestor property like a[2][1]=1 since 1 is an ancestor of 2 ….

i solved it by using extra space an array …the solution i came up is

int a[n][n]={0};
void updatematrix(int a[][n],struct node *root,int temp[],int index){

if(root == NULL)
  return ;
int i;

for(i=0;i< index;i++)
  a[root->data][temp[i]]=1;
temp[index]=root->data;

updatematrix(a,root->left,temp,index+1);
updatematrix(a,root->right,temp,index+1);
}

is there any mistake in my solution ?
can we do this inplace ???(i mean without using the temp array )

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T02:31:54+00:00Added an answer on June 12, 2026 at 2:31 am

    temp contains the path from root to current node, i.e. the set of nodes visited while walking down the tree to arrive at the current node.

    If you have a parent pointer in each node (but I guess not), you can follow those pointers and walk up the tree to traverse the same set of nodes as temp. But this uses more memory.

    You can also walk down the tree several times (pseudocode):

    def updatematrix(a,node):
      if node is null: return
      walkDown(a.data,node.left)
      walkDown(a.data,node.right)
      updatematrix(a,node.left)
      updatematrix(a,node.right)
    
    def walkDown(data,node):
      if node is null: return
      a[node][data] = 1
      walkDown(data,node.left)
      walkDown(data,node.right)
    

    Same complexity, but the pattern of memory access looks less cache friendly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a binary search tree and an integer K, i would like to find
Given a binary search tree, i need to convert it into a doubly linked
We are given a binary search tree; we need to find out its border.
Given a binary search tree (BST). Find the maximum depth of the binary search
I need to generate a series of N random binary variables with a given
Given a simple binary tree, how can we prove that tree is a binary
For a given binary tree, find the largest subtree which is also binary search
I need a tree structure that supports and and oring. For example, given a
Given a binary tree with n leaves and a set of C colors. Each
1) Given 2 arrays containing elements of a complete Binary tree(level by level), without

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.