Non-leaf nodes of a binary tree
Write a function that will return the number of non-leaf nodes in a binary tree. For example: The below binary tree A / \ B C / \ D E has 3 leaf nodes (C, D & E) and 2 non-leaf nodes(A & B).
Making tree from traversals
Given the pre-order and in-order traversal of a Binary Tree. Construct the tree from these traversals. InOrder: 1 4 5 8 10 30 40 PreOrder: 10 5 4 1 8 30 40 Can you construct the Tree if only post-order and in-order traversals are given? Can you construct a tree if only pre-order and post-order […]
Basic Tree Traversals (Preorder, Inorder, Postorder)
If the node of a Binary tree is defined as below: struct Node{ Node * lptr; // Pointer to Left subtree int data; Node *rptr; // Pointer to Right subtree }; write functions which will traverse the tree in in-Order, Pre-Order and Post-Order.
Has Path Sum (binary tree)
Given a Binary Tree and a number x, Write a function to see if there exist a root to leaf path in the tree whose sum is equal to x.
Counting leaf nodes of a Binary Tree
Write a function to count the leaf nodes of a binary tree.
Check if a Binary tree is sub-tree of another
Given pointers to the root of two Binary Trees, write code to check if first Binary tree is subtree of second binary tree. The signature (prototype) of the function should be /** returns 1 (true) if Binary Tree a is subtree of Binary tree b , * else returns 0(false) */ int checkSubTree(struct Node* a, […]
Node with minimum value in a Binary Search Tree
Given a Binary Search Tree (BST), where will you find the node containing minimum value in the Tree? For example: If the tree is as given below Then answer is 1. Write a function that returns the least value in given BST.
Check if a Binary Tree is a Sum Tree or not
A Sum-Tree is a Binary tree, where each non-leaf node has a value equal to the sum of its children. For example: the below tree is a Sum-Tree. Write a function that checks whether a given tree is a Sum-Tree or not.
Inorder successor of node in a Binary Tree
Given a pointer to the node in a binary tree, write code to find its inorder successor. You may assume each node to also have a pointer to the parent node (along with pointer to right and left sub-trees). Hence, the structure of Node will be struct Node { int data; Node* lptr; // pointer […]