Construct Binary Tree from Inorder and Level Order Traversal

Problem Given a inorder and level order traversal, construct a binary tree from that. Examples Example 1: 3 / \ 9 20 / \ 15 7 ...

Evaluation of Arithmetic Expression Tree

Problem Given a simple expression tree, consisting of basic binary operators i.e., + , – , * and / and some integers, evaluate the expression tree. Examples Example 1: graph TD; A(+) --- B(*) & C("-") B --- D(5) & E(4) C --- F(100) & G(20) Input: root = ["+", "*", "-", "5", "4", "100", "20"] Output: 100 ...

Create linked lists of all the nodes at each depth for a Binary Tree

Problem Given a binary search tree, design an algorithm which creates a linked list of all the nodes at each depth (i.e., if you have a tree with depth D, you’ll have D linked lists) Examples Example 1: Input: 1 / \ 2 3 / \ / \ 4 5 6 7 Output: (1) => NULL (2) => (3) => NULL (4) => (5) => (6) => (7) => NULL ...

Diameter Of a Binary Tree

Problem Given a binary tree, find the diameter of it. Definition Diameter of tree is defined as a longest path or route between any two nodes in a tree. The path may or may not for through the root. Example Example 1: 1 / \ 2 3 / \ 4 5 Input: root = [1,2,3,4,5] Output: 3 Explanation: 3 is the length of the path [4,2,1,3] or [5,2,1,3]. ...

Maximum Depth of Binary Tree

Maximum Depth of Binary Tree Problem Given a binary tree, find its maximum depth. Definition A binary tree’s maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. The number of nodes along the longest path from the root node down to the farthest leaf node. Depth of root node is 0. Example Example 1 ...

Populate next pointer to right node in Perfect Binary Tree

Problem You are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree has the following definition: struct Node { int val; Node *left; Node *right; Node *next; } Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL. ...

Same Tree Problem

Problem Given the roots of two binary trees p and q, write a function to check if they are the same or not. Two binary trees are considered the same if they are structurally identical, and the nodes have the same value. Example Input: Two binary Search Trees ...

Sum of Left Leaves Problem

Get the Sum of All Left Leaves in a Binary Tree Problem Objective: Given a binary tree, find the sum of all the nodes which are left as well as leaves nodes. Examples Example 1: graph TD 3 --- 9:::GreenNode & 20 20 --- A[15]:::GreenNode & B[7] classDef GreenNode fill:#0000ff Input: root = [3,9,20,null,null,15,7] Output: 24 Explanation: There are two left leaves in the binary tree, with values 9 and 15 respectively. ...

Binary Tree DFS Traversal

Binary Tree Traversal - Depth First Search DFS Problem Given a Binary Search Tree, Do the Depth First Search/Traversal . Solution In a Binary Tree, we can do 3 kinds of DFS: Inorder Traversal (Left-Root-Right) - Binary Tree Inorder Traversal Preorder Traversal (Root-Left-Right) - Binary Tree Preorder Traversal Morris Traversal - Inorder Tree Traversal without recursion and without stack but using constant space Postorder Traversal (Left-Right-Root) - Binary Tree Postorder Traversal

Binary Tree Level Order Traversal - Level by Level

Problem Given the root of a binary tree, return the level order traversal of its nodes’ values. (i.e., from left to right, level by level). NOTE : This problem is very similar to - Create linked lists of all the nodes at each depth for a Binary Tree Examples Example 1: 3 / \ 9 20 / \ 15 7 ...

This site uses cookies to improve your experience on our website. By using and continuing to navigate this website, you accept this. Privacy Policy