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

  • SEARCH
  • Home
  • 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 8874805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:48:56+00:00 2026-06-14T18:48:56+00:00

The question is, given a binary tree, where each node has four pieces of

  • 0

The question is, given a binary tree, where each node has four pieces of data: left, right ,data and an empty pointer parent, we have to update the tree such that the parent pointer of each node points to its parent (the root parent pointer will naturally point to a NULL value). Now how do I do this? I tried a post-order traversal like this:

last = None
def mypostorder(root):
    if root:
      mypostorder(root.left)
      mypostorder(root.right)
      if last:
        last.parent = root
      last = root

But obviously it is not working, and I know why, after updating the parent pointer of a left-child, it sets it as last, so next time when it visits the right-child (its sibling), it sets its parent to the left-child. How to tweak it to get the correct result? Is it possible to do it iteratively as well, using a stack, may be?

  • 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-14T18:48:57+00:00Added an answer on June 14, 2026 at 6:48 pm

    You have it going the wrong way. I think your solution would make the parent of a node point to its child

    Give this a try:

    def myPostOrder(root, par=None):
        if root:
            root.parent = par
            myPostOrder(root.left, root)
            myPostOrder(root.right, root)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I found an interesting algorithmic problem. We are given a binary tree which has
The question is how to create a binary tree, given its ancestor matrix. I
This is a homework question, binary search has already been introduced: Given two arrays,
The following is an interview question. You are given a binary tree (not necessarily
I was asked this question: You are given two lists of integers, each of
I'm trying to construct a binary tree (unbalanced), given its traversals. I'm currently doing
I've given only a pre-order traversal sequence of a binary tree (e.g. {a, b,
How do I find the density of a given binary tree? I came across
Given a binary search tree, in which any two nodes are swapped. So we
Is it possible to construct a Binary Search Tree Given only its preorder traversal

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.