• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • Tutorial- C/C++
    • Tutorial- Django
    • Tutorial- Git
    • Tutorial- HTML & CSS
    • Tutorial- Java
    • Tutorial- MySQL
    • Tutorial- Python
    • Competitive Coding Challenges
  • CSE Subject
    • (CD) Compiler Design
    • (CN) Computer Network
    • (COA) Computer Organization & Architecture
    • (DBMS) Database Management System
    • (DS) Data Structure
    • (OS) Operating System
    • (ToA) Theory of Automata
    • (WT) Web Technology
  • Interview Questions
    • Interview Questions- Company Wise
    • Interview Questions- Coding Round
    • Interview Questions- Python
    • Interview Questions- REST API
    • Interview Questions- Web Scraping
    • Interview Questions- HR Round
    • Aptitude Preparation Guide
  • GATE 2024
  • Linux
  • Trend
    • Full Stack Development
    • Artificial Intelligence (AI)
    • BigData
    • Cloud Computing
    • Machine Learning (ML)
  • Write for Us
    • Submit Article
    • Submit Source Code or Program
    • Share Your Interview Experience
  • Tools
    • IDE
    • CV Builder
    • Other Tools …
  • Jobs

Find Parent of a Node in Binary Tree | Python Code

Aniruddha Chaudhari/555/0
CodePython

Problem statement:

Write a program to find the parent of the node in binary tree (BT).

(This question is asked in many coding interview rounds.)

Example:

binary tree

Let’s take the example of the above binary tree.

The parent of node 3 is 1.
The parent of node 5 is 4.
The parent of node 6 is 5.

You can solve this in any programming language like C/C++, Java or Python.

Here I’m writing a Python program to find the parent of the given node in a binary tree.

Python Solution

Before we find parent of a node in binary tree, we have to create the binary tree using Python. And it is simple.

Prerequiste:

  • How to create a binary tree in Python?

To find the parent of the given node in BT, we are using recursion.

Python code:

class node:
    def __init__(self, val, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

root = node(4, node(1, node(7), node(3)), node(5,None,node(6)))

def parent(obj, val):
    if obj:
        if (obj.left and obj.left.val==val) or (obj.right and obj.right.val==val):
            return obj.val or parent(obj.left, val) or parent(obj.right, val)
        else:
            return parent(obj.left, val) or parent(obj.right, val)


print(f"The parent of node 3 is {parent(root, 3)}.")

Output:

The parent of node 3 is 1.

With little modification, you can also print and find all the ancestors of the given node in the binary tree.

If you have any questions or doubts, feel free to put that in the comment section below. You can also provide an alternate solution to the problem.

Python Interview Questions eBook

Binary TreePython
Aniruddha Chaudhari
I am complete Python Nut, love Linux and vim as an editor. I hold a Master of Computer Science from NIT Trichy. I dabble in C/C++, Java too. I keep sharing my coding knowledge and my own experience on CSEstack.org portal.

Your name can also be listed here. Got a tip? Submit it here to become an CSEstack author.

Leave a Reply Cancel reply

Binary Tree (BT)

  1. BT- Introduction
  2. BT- Create Binary Tree
  3. BT- Pre/In/Post Order Traversal
  4. BT- Height 
  5. BT- Level Order Traversal
  6. BT- Min and Max Heap
  7. BT- Parent of a Given Node
  8. BT- Ancestors of Given Node
  9. BT- Lowest Common Ancestor
  10. BT- Check Sub Tree

Binary Search Tree (BST)

  1. BST- Introduction
  2. BST- Search Node
  3. BST- Insert Node

© 2022 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard