Find maximum among all right nodes in Binary Tree

Here given code implementation process.
// Java program for
// Find maximum among all right nodes in Binary Tree
class TreeNode
{
// Data value
public int data;
// Indicates left and right subtree
public TreeNode left;
public TreeNode right;
public TreeNode(int data)
{
this.data = data;
this.left = null;
this.right = null;
}
}
public class BinaryTree
{
public TreeNode root;
public TreeNode result;
public BinaryTree()
{
this.root = null;
this.result = null;
}
public void findMaxRightNode(TreeNode node)
{
if (node != null)
{
if (node.right != null)
{
// When current node have a right child exists
if (this.result == null ||
this.result.data < node.right.data)
{
// Update result node
this.result = node.right;
}
}
// Visit left subtree
findMaxRightNode(node.left);
// Visit right subtree
findMaxRightNode(node.right);
}
}
public void maxRightChild()
{
this.result = null;
this.findMaxRightNode(this.root);
if (this.result == null)
{
// When no right child
System.out.println("None");
}
else
{
// Display calculated result
System.out.println(this.result.data);
}
}
public static void main(String[] args)
{
BinaryTree tree = new BinaryTree();
/* Binary Tree
-----------------------
1
/ \
11 3
/ / \
4 5 6
/ \ \ \
8 7 12 -4
/ \
-2 9
*/
// Add Binary tree nodes
tree.root = new TreeNode(1);
tree.root.left = new TreeNode(11);
tree.root.right = new TreeNode(3);
tree.root.right.right = new TreeNode(6);
tree.root.right.right.right = new TreeNode(-4);
tree.root.right.left = new TreeNode(5);
tree.root.left.left = new TreeNode(4);
tree.root.left.left.left = new TreeNode(8);
tree.root.left.left.right = new TreeNode(7);
tree.root.left.left.right.left = new TreeNode(-2);
tree.root.left.left.right.right = new TreeNode(9);
tree.root.right.left.right = new TreeNode(12);
/*
1
/ \
11 3
/ / \
4 5 6
/ \ \ \
8 7 12 -4
/ \ ↑
-2 9
----------------------
Max right child node is : 12
*/
tree.maxRightChild();
}
}
Output
12
// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Find maximum among all right nodes in Binary Tree
class TreeNode
{
public:
// Data value
int data;
// Indicates left and right subtree
TreeNode *left;
TreeNode *right;
TreeNode(int data)
{
this->data = data;
this->left = NULL;
this->right = NULL;
}
};
class BinaryTree
{
public:
TreeNode *root;
TreeNode *result;
BinaryTree()
{
this->root = NULL;
this->result = NULL;
}
void findMaxRightNode(TreeNode *node)
{
if (node != NULL)
{
if (node->right != NULL)
{
// When current node have a right child exists
if (this->result == NULL ||
this->result->data < node->right->data)
{
// Update result node
this->result = node->right;
}
}
// Visit left subtree
this->findMaxRightNode(node->left);
// Visit right subtree
this->findMaxRightNode(node->right);
}
}
void maxRightChild()
{
this->result = NULL;
this->findMaxRightNode(this->root);
if (this->result == NULL)
{
// When no right child
cout << "None" << endl;
}
else
{
// Display calculated result
cout << this->result->data << endl;
}
}
};
int main()
{
BinaryTree *tree = new BinaryTree();
/*
Binary Tree
-----------------------
1
/ \
11 3
/ / \
4 5 6
/ \ \ \
8 7 12 -4
/ \
-2 9
*/
// Add Binary tree nodes
tree->root = new TreeNode(1);
tree->root->left = new TreeNode(11);
tree->root->right = new TreeNode(3);
tree->root->right->right = new TreeNode(6);
tree->root->right->right->right = new TreeNode(-4);
tree->root->right->left = new TreeNode(5);
tree->root->left->left = new TreeNode(4);
tree->root->left->left->left = new TreeNode(8);
tree->root->left->left->right = new TreeNode(7);
tree->root->left->left->right->left = new TreeNode(-2);
tree->root->left->left->right->right = new TreeNode(9);
tree->root->right->left->right = new TreeNode(12);
/*
1
/ \
11 3
/ / \
4 5 6
/ \ \ \
8 7 12 -4
/ \ ↑
-2 9
----------------------
Max right child node is : 12
*/
tree->maxRightChild();
return 0;
}
Output
12
package main
import "fmt"
// Go program for
// Find maximum among all right nodes in Binary Tree
type TreeNode struct {
// Data value
data int
// Indicates left and right subtree
left * TreeNode
right * TreeNode
}
func getTreeNode(data int) * TreeNode {
var me *TreeNode = &TreeNode {}
me.data = data
me.left = nil
me.right = nil
return me
}
type BinaryTree struct {
root * TreeNode
result * TreeNode
}
func getBinaryTree() * BinaryTree {
var me *BinaryTree = &BinaryTree {}
me.root = nil
me.result = nil
return me
}
func(this *BinaryTree) findMaxRightNode(node * TreeNode) {
if node != nil {
if node.right != nil {
// When current node have a right child exists
if this.result == nil ||
this.result.data < node.right.data {
// Update result node
this.result = node.right
}
}
// Visit left subtree
this.findMaxRightNode(node.left)
// Visit right subtree
this.findMaxRightNode(node.right)
}
}
func(this *BinaryTree) maxRightChild() {
this.result = nil
this.findMaxRightNode(this.root)
if this.result == nil {
// When no right child
fmt.Println("None")
} else {
// Display calculated result
fmt.Println(this.result.data)
}
}
func main() {
var tree * BinaryTree = getBinaryTree()
/*
Binary Tree
-----------------------
1
/ \
11 3
/ / \
4 5 6
/ \ \ \
8 7 12 -4
/ \
-2 9
*/
// Add Binary tree nodes
tree.root = getTreeNode(1)
tree.root.left = getTreeNode(11)
tree.root.right = getTreeNode(3)
tree.root.right.right = getTreeNode(6)
tree.root.right.right.right = getTreeNode(-4)
tree.root.right.left = getTreeNode(5)
tree.root.left.left = getTreeNode(4)
tree.root.left.left.left = getTreeNode(8)
tree.root.left.left.right = getTreeNode(7)
tree.root.left.left.right.left = getTreeNode(-2)
tree.root.left.left.right.right = getTreeNode(9)
tree.root.right.left.right = getTreeNode(12)
/*
1
/ \
11 3
/ / \
4 5 6
/ \ \ \
8 7 12 -4
/ \ ↑
-2 9
----------------------
Max right child node is : 12
*/
tree.maxRightChild()
}
Output
12
// Include namespace system
using System;
// Csharp program for
// Find maximum among all right nodes in Binary Tree
public class TreeNode
{
// Data value
public int data;
// Indicates left and right subtree
public TreeNode left;
public TreeNode right;
public TreeNode(int data)
{
this.data = data;
this.left = null;
this.right = null;
}
}
public class BinaryTree
{
public TreeNode root;
public TreeNode result;
public BinaryTree()
{
this.root = null;
this.result = null;
}
public void findMaxRightNode(TreeNode node)
{
if (node != null)
{
if (node.right != null)
{
// When current node have a right child exists
if (this.result == null ||
this.result.data < node.right.data)
{
// Update result node
this.result = node.right;
}
}
// Visit left subtree
this.findMaxRightNode(node.left);
// Visit right subtree
this.findMaxRightNode(node.right);
}
}
public void maxRightChild()
{
this.result = null;
this.findMaxRightNode(this.root);
if (this.result == null)
{
// When no right child
Console.WriteLine("None");
}
else
{
// Display calculated result
Console.WriteLine(this.result.data);
}
}
public static void Main(String[] args)
{
BinaryTree tree = new BinaryTree();
/*
Binary Tree
-----------------------
1
/ \
11 3
/ / \
4 5 6
/ \ \ \
8 7 12 -4
/ \
-2 9
*/
// Add Binary tree nodes
tree.root = new TreeNode(1);
tree.root.left = new TreeNode(11);
tree.root.right = new TreeNode(3);
tree.root.right.right = new TreeNode(6);
tree.root.right.right.right = new TreeNode(-4);
tree.root.right.left = new TreeNode(5);
tree.root.left.left = new TreeNode(4);
tree.root.left.left.left = new TreeNode(8);
tree.root.left.left.right = new TreeNode(7);
tree.root.left.left.right.left = new TreeNode(-2);
tree.root.left.left.right.right = new TreeNode(9);
tree.root.right.left.right = new TreeNode(12);
/*
1
/ \
11 3
/ / \
4 5 6
/ \ \ \
8 7 12 -4
/ \ ↑
-2 9
----------------------
Max right child node is : 12
*/
tree.maxRightChild();
}
}
Output
12
<?php
// Php program for
// Find maximum among all right nodes in Binary Tree
class TreeNode
{
// Data value
public $data;
// Indicates left and right subtree
public $left;
public $right;
public function __construct($data)
{
$this->data = $data;
$this->left = NULL;
$this->right = NULL;
}
}
class BinaryTree
{
public $root;
public $result;
public function __construct()
{
$this->root = NULL;
$this->result = NULL;
}
public function findMaxRightNode($node)
{
if ($node != NULL)
{
if ($node->right != NULL)
{
// When current node have a right child exists
if ($this->result == NULL ||
$this->result->data < $node->right->data)
{
// Update result node
$this->result = $node->right;
}
}
// Visit left subtree
$this->findMaxRightNode($node->left);
// Visit right subtree
$this->findMaxRightNode($node->right);
}
}
public function maxRightChild()
{
$this->result = NULL;
$this->findMaxRightNode($this->root);
if ($this->result == NULL)
{
// When no right child
echo("None\n");
}
else
{
// Display calculated result
echo($this->result->data."\n");
}
}
}
function main()
{
$tree = new BinaryTree();
/*
Binary Tree
-----------------------
1
/ \
11 3
/ / \
4 5 6
/ \ \ \
8 7 12 -4
/ \
-2 9
*/
// Add Binary tree nodes
$tree->root = new TreeNode(1);
$tree->root->left = new TreeNode(11);
$tree->root->right = new TreeNode(3);
$tree->root->right->right = new TreeNode(6);
$tree->root->right->right->right = new TreeNode(-4);
$tree->root->right->left = new TreeNode(5);
$tree->root->left->left = new TreeNode(4);
$tree->root->left->left->left = new TreeNode(8);
$tree->root->left->left->right = new TreeNode(7);
$tree->root->left->left->right->left = new TreeNode(-2);
$tree->root->left->left->right->right = new TreeNode(9);
$tree->root->right->left->right = new TreeNode(12);
/*
1
/ \
11 3
/ / \
4 5 6
/ \ \ \
8 7 12 -4
/ \ ↑
-2 9
----------------------
Max right child node is : 12
*/
$tree->maxRightChild();
}
main();
Output
12
// Node JS program for
// Find maximum among all right nodes in Binary Tree
class TreeNode
{
constructor(data)
{
this.data = data;
this.left = null;
this.right = null;
}
}
class BinaryTree
{
constructor()
{
this.root = null;
this.result = null;
}
findMaxRightNode(node)
{
if (node != null)
{
if (node.right != null)
{
// When current node have a right child exists
if (this.result == null ||
this.result.data < node.right.data)
{
// Update result node
this.result = node.right;
}
}
// Visit left subtree
this.findMaxRightNode(node.left);
// Visit right subtree
this.findMaxRightNode(node.right);
}
}
maxRightChild()
{
this.result = null;
this.findMaxRightNode(this.root);
if (this.result == null)
{
// When no right child
console.log("None");
}
else
{
// Display calculated result
console.log(this.result.data);
}
}
}
function main()
{
var tree = new BinaryTree();
/*
Binary Tree
-----------------------
1
/ \
11 3
/ / \
4 5 6
/ \ \ \
8 7 12 -4
/ \
-2 9
*/
// Add Binary tree nodes
tree.root = new TreeNode(1);
tree.root.left = new TreeNode(11);
tree.root.right = new TreeNode(3);
tree.root.right.right = new TreeNode(6);
tree.root.right.right.right = new TreeNode(-4);
tree.root.right.left = new TreeNode(5);
tree.root.left.left = new TreeNode(4);
tree.root.left.left.left = new TreeNode(8);
tree.root.left.left.right = new TreeNode(7);
tree.root.left.left.right.left = new TreeNode(-2);
tree.root.left.left.right.right = new TreeNode(9);
tree.root.right.left.right = new TreeNode(12);
/*
1
/ \
11 3
/ / \
4 5 6
/ \ \ \
8 7 12 -4
/ \ ↑
-2 9
----------------------
Max right child node is : 12
*/
tree.maxRightChild();
}
main();
Output
12
# Python 3 program for
# Find maximum among all right nodes in Binary Tree
class TreeNode :
# Data value
# Indicates left and right subtree
def __init__(self, data) :
self.data = data
self.left = None
self.right = None
class BinaryTree :
def __init__(self) :
self.root = None
self.result = None
def findMaxRightNode(self, node) :
if (node != None) :
if (node.right != None) :
# When current node have a right child exists
if (self.result == None or self.result.data < node.right.data) :
# Update result node
self.result = node.right
# Visit left subtree
self.findMaxRightNode(node.left)
# Visit right subtree
self.findMaxRightNode(node.right)
def maxRightChild(self) :
self.result = None
self.findMaxRightNode(self.root)
if (self.result == None) :
# When no right child
print("None")
else :
# Display calculated result
print(self.result.data)
def main() :
tree = BinaryTree()
# Binary Tree
# -----------------------
# 1
# / \
# 11 3
# / / \
# 4 5 6
# / \ \ \
# 8 7 12 -4
# / \
# -2 9
# Add Binary tree nodes
tree.root = TreeNode(1)
tree.root.left = TreeNode(11)
tree.root.right = TreeNode(3)
tree.root.right.right = TreeNode(6)
tree.root.right.right.right = TreeNode(-4)
tree.root.right.left = TreeNode(5)
tree.root.left.left = TreeNode(4)
tree.root.left.left.left = TreeNode(8)
tree.root.left.left.right = TreeNode(7)
tree.root.left.left.right.left = TreeNode(-2)
tree.root.left.left.right.right = TreeNode(9)
tree.root.right.left.right = TreeNode(12)
# 1
# / \
# 11 3
# / / \
# 4 5 6
# / \ \ \
# 8 7 12 -4
# / \ ↑
# -2 9
# ----------------------
# Max right child node is : 12
tree.maxRightChild()
if __name__ == "__main__": main()
Output
12
# Ruby program for
# Find maximum among all right nodes in Binary Tree
class TreeNode
# Define the accessor and reader of class TreeNode
attr_reader :data, :left, :right
attr_accessor :data, :left, :right
# Data value
# Indicates left and right subtree
def initialize(data)
self.data = data
self.left = nil
self.right = nil
end
end
class BinaryTree
# Define the accessor and reader of class BinaryTree
attr_reader :root, :result
attr_accessor :root, :result
def initialize()
self.root = nil
self.result = nil
end
def findMaxRightNode(node)
if (node != nil)
if (node.right != nil)
# When current node have a right child exists
if (self.result == nil ||
self.result.data < node.right.data)
# Update result node
self.result = node.right
end
end
# Visit left subtree
self.findMaxRightNode(node.left)
# Visit right subtree
self.findMaxRightNode(node.right)
end
end
def maxRightChild()
self.result = nil
self.findMaxRightNode(self.root)
if (self.result == nil)
# When no right child
print("None", "\n")
else
# Display calculated result
print(self.result.data, "\n")
end
end
end
def main()
tree = BinaryTree.new()
# Binary Tree
# -----------------------
# 1
# / \
# 11 3
# / / \
# 4 5 6
# / \ \ \
# 8 7 12 -4
# / \
# -2 9
# Add Binary tree nodes
tree.root = TreeNode.new(1)
tree.root.left = TreeNode.new(11)
tree.root.right = TreeNode.new(3)
tree.root.right.right = TreeNode.new(6)
tree.root.right.right.right = TreeNode.new(-4)
tree.root.right.left = TreeNode.new(5)
tree.root.left.left = TreeNode.new(4)
tree.root.left.left.left = TreeNode.new(8)
tree.root.left.left.right = TreeNode.new(7)
tree.root.left.left.right.left = TreeNode.new(-2)
tree.root.left.left.right.right = TreeNode.new(9)
tree.root.right.left.right = TreeNode.new(12)
# 1
# / \
# 11 3
# / / \
# 4 5 6
# / \ \ \
# 8 7 12 -4
# / \ ↑
# -2 9
# ----------------------
# Max right child node is : 12
tree.maxRightChild()
end
main()
Output
12
// Scala program for
// Find maximum among all right nodes in Binary Tree
class TreeNode(
// Data value
var data: Int,
// Indicates left and right subtree
var left: TreeNode,
var right: TreeNode)
{
def this(data: Int)
{
this(data, null, null);
}
}
class BinaryTree(var root: TreeNode,
var result: TreeNode)
{
def this()
{
this(null, null);
}
def findMaxRightNode(node: TreeNode): Unit = {
if (node != null)
{
if (node.right != null)
{
// When current node have a right child exists
if (this.result == null ||
this.result.data < node.right.data)
{
// Update result node
this.result = node.right;
}
}
// Visit left subtree
findMaxRightNode(node.left);
// Visit right subtree
findMaxRightNode(node.right);
}
}
def maxRightChild(): Unit = {
this.result = null;
this.findMaxRightNode(this.root);
if (this.result == null)
{
// When no right child
println("None");
}
else
{
// Display calculated result
println(this.result.data);
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var tree: BinaryTree = new BinaryTree();
/*
Binary Tree
-----------------------
1
/ \
11 3
/ / \
4 5 6
/ \ \ \
8 7 12 -4
/ \
-2 9
*/
// Add Binary tree nodes
tree.root = new TreeNode(1);
tree.root.left = new TreeNode(11);
tree.root.right = new TreeNode(3);
tree.root.right.right = new TreeNode(6);
tree.root.right.right.right = new TreeNode(-4);
tree.root.right.left = new TreeNode(5);
tree.root.left.left = new TreeNode(4);
tree.root.left.left.left = new TreeNode(8);
tree.root.left.left.right = new TreeNode(7);
tree.root.left.left.right.left = new TreeNode(-2);
tree.root.left.left.right.right = new TreeNode(9);
tree.root.right.left.right = new TreeNode(12);
/*
1
/ \
11 3
/ / \
4 5 6
/ \ \ \
8 7 12 -4
/ \ ↑
-2 9
----------------------
Max right child node is : 12
*/
tree.maxRightChild();
}
}
Output
12
// Swift 4 program for
// Find maximum among all right nodes in Binary Tree
class TreeNode
{
// Data value
var data: Int;
// Indicates left and right subtree
var left: TreeNode? ;
var right: TreeNode? ;
init(_ data: Int)
{
self.data = data;
self.left = nil;
self.right = nil;
}
}
class BinaryTree
{
var root: TreeNode? ;
var result: TreeNode? ;
init()
{
self.root = nil;
self.result = nil;
}
func findMaxRightNode(_ node: TreeNode? )
{
if (node != nil)
{
if (node!.right != nil)
{
// When current node have a right child exists
if (self.result == nil ||
self.result!.data < node!.right!.data)
{
// Update result node
self.result = node!.right;
}
}
// Visit left subtree
self.findMaxRightNode(node!.left);
// Visit right subtree
self.findMaxRightNode(node!.right);
}
}
func maxRightChild()
{
self.result = nil;
self.findMaxRightNode(self.root);
if (self.result == nil)
{
// When no right child
print("None");
}
else
{
// Display calculated result
print(self.result!.data);
}
}
}
func main()
{
let tree: BinaryTree = BinaryTree();
/*
Binary Tree
-----------------------
1
/ \
11 3
/ / \
4 5 6
/ \ \ \
8 7 12 -4
/ \
-2 9
*/
// Add Binary tree nodes
tree.root = TreeNode(1);
tree.root!.left = TreeNode(11);
tree.root!.right = TreeNode(3);
tree.root!.right!.right = TreeNode(6);
tree.root!.right!.right!.right = TreeNode(-4);
tree.root!.right!.left = TreeNode(5);
tree.root!.left!.left = TreeNode(4);
tree.root!.left!.left!.left = TreeNode(8);
tree.root!.left!.left!.right = TreeNode(7);
tree.root!.left!.left!.right!.left = TreeNode(-2);
tree.root!.left!.left!.right!.right = TreeNode(9);
tree.root!.right!.left!.right = TreeNode(12);
/*
1
/ \
11 3
/ / \
4 5 6
/ \ \ \
8 7 12 -4
/ \ ↑
-2 9
----------------------
Max right child node is : 12
*/
tree.maxRightChild();
}
main();
Output
12
// Kotlin program for
// Find maximum among all right nodes in Binary Tree
class TreeNode
{
// Data value
var data: Int;
// Indicates left and right subtree
var left: TreeNode ? ;
var right: TreeNode ? ;
constructor(data: Int)
{
this.data = data;
this.left = null;
this.right = null;
}
}
class BinaryTree
{
var root: TreeNode ? ;
var result: TreeNode ? ;
constructor()
{
this.root = null;
this.result = null;
}
fun findMaxRightNode(node: TreeNode ? ): Unit
{
if (node != null)
{
if (node.right != null)
{
// When current node have a right child exists
if (this.result == null ||
this.result!!.data < node.right!!.data)
{
// Update result node
this.result = node.right;
}
}
// Visit left subtree
this.findMaxRightNode(node.left);
// Visit right subtree
this.findMaxRightNode(node.right);
}
}
fun maxRightChild(): Unit
{
this.result = null;
this.findMaxRightNode(this.root);
if (this.result == null)
{
// When no right child
println("None");
}
else
{
// Display calculated result
println(this.result?.data);
}
}
}
fun main(args: Array < String > ): Unit
{
val tree: BinaryTree = BinaryTree();
/*
Binary Tree
-----------------------
1
/ \
11 3
/ / \
4 5 6
/ \ \ \
8 7 12 -4
/ \
-2 9
*/
// Add Binary tree nodes
tree.root = TreeNode(1);
tree.root?.left = TreeNode(11);
tree.root?.right = TreeNode(3);
tree.root?.right?.right = TreeNode(6);
tree.root?.right?.right?.right = TreeNode(-4);
tree.root?.right?.left = TreeNode(5);
tree.root?.left?.left = TreeNode(4);
tree.root?.left?.left?.left = TreeNode(8);
tree.root?.left?.left?.right = TreeNode(7);
tree.root?.left?.left?.right?.left = TreeNode(-2);
tree.root?.left?.left?.right?.right = TreeNode(9);
tree.root?.right?.left?.right = TreeNode(12);
/*
1
/ \
11 3
/ / \
4 5 6
/ \ \ \
8 7 12 -4
/ \ ↑
-2 9
----------------------
Max right child node is : 12
*/
tree.maxRightChild();
}
Output
12
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment