- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to find sibling value of a binary tree node in Python
Suppose we have a value k and a binary search tree, here each node is either a leaf or contains 2 children. We have to find the node containing the value k, and return its sibling's value.
So, if the input is like
k = 4., then the output will be 10.
To solve this, we will follow these steps −
Define a function util() . This will take root, k, ans
if left of root is not null and right of root is not null, then
return
if k > value of root, then
if value of right of root is same as k, then
insert value of left of root at the end of ans
return
otherwise,
util(right of root, k, ans)
if k < value of root, then
if value of right of root is same as k, then
insert value of right of root at the end of ans
return
otherwise,
util(left of root, k, ans)
From the main method, do the following −
ans := a new list
util(root, k, ans)
return ans[0]
Let us see the following implementation to get better understanding −
Example
class TreeNode: def __init__(self, data, left = None, right = None): self.val = data self.left = left self.right = right def util(root, k, ans): if root.left is None and root.right is None: return if k > root.val: if root.right.val == k: ans.append(root.left.val) return else: util(root.right, k, ans) if k < root.val: if root.left.val == k: ans.append(root.right.val) return else: util(root.left, k, ans) class Solution: def solve(self, root, k): ans = [] util(root, k, ans) return ans[0] root = TreeNode(6) root.left = TreeNode(4) root.right = TreeNode(10) root.left.left = TreeNode(3) root.left.right = TreeNode(5) ob1 = Solution() print(ob1.solve(root, 4))
Input
root = TreeNode(6) root.left = TreeNode(4) root.right = TreeNode(10) root.left.left = TreeNode(3) root.left.right = TreeNode(5) 4
Output
10
- Related Articles
- Program to find second deepest node in a binary tree in python
- Program to find out the node in the right in a binary tree using Python
- Program to find longest even value path of a binary tree in Python
- Find right sibling of a binary tree with parent pointers in C++
- Program to find leftmost deepest node of a tree in Python
- Program to find Kth ancestor of a tree node in Python
- Find the node with minimum value in a Binary Search Tree in C++
- Program to find top view of a binary tree in Python
- Find mirror of a given node in Binary tree in C++
- Python Program to Find Nth Node in the Inorder Traversal of a Tree
- C++ program to Replace a Node with Depth in a Binary Tree
- Find a Corresponding Node of a Binary Tree in a Clone of That Tree in C++
- Find the Deepest Node in a Binary Tree in C++
- Program to find out the largest sum value of a BST in a given binary tree in Python
- Program to find the maximum width of a binary tree in Python
