
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
Program to find out the node in the right in a binary tree using Python
Suppose, we are provided a binary tree. We are also given a pointer to a node (named ‘u’) and we have to find the node situated just right of the provided node. The node situated to the given node's right must stay at the same level and the given node can either be a leaf node or an internal node.
So, if the input is like
and u = 6, then the output will be 8.
The node situated at the right of node 6 is node 8, so the value 8 is returned to us.
To solve this, we will follow these steps −
if root is empty, then
return null
dq := a new deque
insert root at the end of dq
while dq is not empty, do
dq_size := size of dq
temp := a new list
index := -1
for each value in range 0 to dq_size, do
node := delete last element from dq
if left of node is not empty, then
add left of node to the end of dq
if right of node is not empty, then
add right of node to the end of dq
insert node at the end of temp
if node is same as u, then
index := size of temp - 1
if index is same as size of temp - 1, then
return null
if index > -1, then
return temp[index + 1]
return null
Let us see the following implementation to get better understanding −
Example
from queue import deque class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right def insert(temp,data): que = [] que.append(temp) while (len(que)): temp = que[0] que.pop(0) if (not temp.left): if data is not None: temp.left = TreeNode(data) else: temp.left = TreeNode(0) break else: que.append(temp.left) if (not temp.right): if data is not None: temp.right = TreeNode(data) else: temp.right = TreeNode(0) break else: que.append(temp.right) def make_tree(elements): Tree = TreeNode(elements[0]) for element in elements[1:]: insert(Tree, element) return Tree def search_node(root, element): if (root == None): return None if (root.val == element): return root res1 = search_node(root.left, element) if res1: return res1 res2 = search_node(root.right, element) return res2 def solve(root, u): if not root: return None dq = deque() dq.append(root) while dq: dq_size = len(dq) temp = [] index = -1 for _ in range(dq_size): node = dq.pop() if node.left: dq.appendleft(node.left) if node.right: dq.appendleft(node.right) temp.append(node) if node == u: index = len(temp) - 1 if index == len(temp) - 1: return None if index > -1: return temp[index + 1] return None root = make_tree([5, 3, 7, 2, 4, 6, 8]) u = search_node(root,6) ret = solve(root, u) print(ret.val)
Input
root = make_tree([5, 3, 7, 2, 4, 6, 8]) u = search_node(root,6)
Output
8
- Related Articles
- Program to find second deepest node in a binary tree in python
- Program to find out the lowest common ancestor of a binary tree using Python
- Program to find sibling value of a binary tree node in Python
- Program to find out the lowest common ancestor of a binary tree using parent pointers using Python
- Find the Deepest Node in a Binary Tree in C++
- Program to find out the lowest common ancestor of a binary tree of given nodes using Python
- Program to find out distance between two nodes in a binary tree in Python
- Program to find out the largest sum value of a BST in a given binary tree in Python
- Program to find out if a BST is present in a given binary tree in Python
- Program to Find Out the Special Nodes in a Tree in Python
- Program to find sum of the right leaves of a binary tree in C++
- Python Program to Find Nth Node in the Inorder Traversal of a Tree
- Program to find out if a linked list is present in a given binary tree in Python
- Program to find leftmost deepest node of a tree in Python
- Program to find Kth ancestor of a tree node in Python
