0% found this document useful (0 votes)
27 views6 pages

Binary Tree Questions

The document discusses three binary tree problems and their solutions: 1) Post-order iterative traversal of a binary tree using two stacks. 2) Printing all nodes that are a distance K from the root by doing a level order traversal and checking the current level. 3) Finding the maximum element in a binary tree by recursively finding the maximum in the left and right subtrees and returning the largest of the three values.

Uploaded by

vinayak mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views6 pages

Binary Tree Questions

The document discusses three binary tree problems and their solutions: 1) Post-order iterative traversal of a binary tree using two stacks. 2) Printing all nodes that are a distance K from the root by doing a level order traversal and checking the current level. 3) Finding the maximum element in a binary tree by recursively finding the maximum in the left and right subtrees and returning the largest of the three values.

Uploaded by

vinayak mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Binary Tree Questions

Name: Kushagra Ojha SapI’D: 100001533

1. Post Order Iterative Solution to be Build

import java.util.Stack;

class TreeNode {

int val;

TreeNode left;

TreeNode right;

TreeNode(int val) {

this.val = val;

this.left = null;

this.right = null;

public class BinaryTree {

// Post-order iterative traversal of the binary tree

public void postOrderIterative(TreeNode root) {

if (root == null)

return;

Stack<TreeNode> stack1 = new Stack<>();

Stack<TreeNode> stack2 = new Stack<>();


stack1.push(root);

while (!stack1.isEmpty()) {

TreeNode current = stack1.pop();

stack2.push(current);

if (current.left != null)

stack1.push(current.left);

if (current.right != null)

stack1.push(current.right);

while (!stack2.isEmpty()) {

TreeNode node = stack2.pop();

System.out.print(node.val + " ");

public static void main(String[] args) {

// Create a binary tree for testing

TreeNode root = new TreeNode(1);

root.left = new TreeNode(2);

root.right = new TreeNode(3);

root.left.left = new TreeNode(4);

root.left.right = new TreeNode(5);

BinaryTree bt = new BinaryTree();

System.out.print("Post-order traversal: ");

bt.postOrderIterative(root);
}

2. Print Nodes at Distance K from the Root

import java.util.LinkedList;

import java.util.Queue;

class TreeNode {

int val;

TreeNode left;

TreeNode right;

TreeNode(int val) {

this.val = val;

this.left = null;

this.right = null;

public class BinaryTree {

// Print nodes at distance K from the root

public void printNodesAtDistanceK(TreeNode root, int k) {

if (root == null || k < 0)

return;

Queue<TreeNode> queue = new LinkedList<>();

queue.offer(root);
int currentLevel = 0;

while (!queue.isEmpty()) {

int nodesAtCurrentLevel = queue.size();

if (currentLevel == k) {

System.out.print("Nodes at distance " + k + ": ");

for (TreeNode node : queue) {

System.out.print(node.val + " ");

System.out.println();

return;

for (int i = 0; i < nodesAtCurrentLevel; i++) {

TreeNode current = queue.poll();

if (current.left != null)

queue.offer(current.left);

if (current.right != null)

queue.offer(current.right);

currentLevel++;

public static void main(String[] args) {

// Create a binary tree for testing

TreeNode root = new TreeNode(1);

root.left = new TreeNode(2);

root.right = new TreeNode(3);


root.left.left = new TreeNode(4);

root.left.right = new TreeNode(5);

root.right.left = new TreeNode(6);

root.right.right = new TreeNode(7);

BinaryTree bt = new BinaryTree();

int k = 2;

bt.printNodesAtDistanceK(root, k);

3. Maximum Element in a Binary Tree.

class TreeNode {

int val;

TreeNode left;

TreeNode right;

TreeNode(int val) {

this.val = val;

this.left = null;

this.right = null;

public class BinaryTree {

// Find the maximum element in the binary tree

public int findMaximum(TreeNode root) {

if (root == null)

return Integer.MIN_VALUE;
int leftMax = findMaximum(root.left);

int rightMax = findMaximum(root.right);

return Math.max(root.val, Math.max(leftMax, rightMax));

public static void main(String[] args) {

// Create a binary tree for testing

TreeNode root = new TreeNode(10);

root.left = new TreeNode(5);

root.right = new TreeNode(15);

root.left.left = new TreeNode(3);

root.left.right = new TreeNode(8);

root.right.right = new TreeNode(20);

BinaryTree bt = new BinaryTree();

int max = bt.findMaximum(root);

System.out.println("Maximum element in the binary tree: " + max);

You might also like