Binary Tree Traversal - Level Order

Problem Given a binary tree, return the level order traversal of its nodes’ values. (ie, from left to right, level by level). Examples Example 1: Given binary tree {3, 9, 20, #, #, 15, 7}, 3 / \ 9 20 / \ 15 7 ...

Find Deepest Node in a Binary Tree

Problem Given a binary tree, find the deepest node in it. If there are multiple, return the right most node. (Good to check with the interviewer - some may say left most). Examples Example 1: 1 / \ 2 3 / \ / \ 4 5 6 7 ...

Subtree of Another Tree Problem

Problem Given the roots of two binary trees root and subRoot, return true if there is a subtree of root with the same structure and node values of subRoot and false otherwise. A subtree of a binary tree tree is a tree that consists of a node in tree and all of this node’s descendants. The tree tree could also be considered as a subtree of itself. Examples Example 1: ...

Meeting Rooms 2 - Minimum Meeting Rooms Required

Problem Given a list of intervals representing the start and end time of ‘N’ meetings, find the minimum number of rooms required to hold all the meetings. Examples Example 1: Meetings: [ [1,4], [2,5], [7,9] ] Output: 2 Explanation: Since [1,4] and [2,5] overlap, we need two rooms to hold these two meetings. [7,9] can occur in any of the two rooms later. ...

Binary Tree Traversal - Level Order Bottom up

Problem Given a binary tree, return the bottom-up level order traversal of its nodes’ values. Examples Example 1: 3 / \ 9 20 / \ 15 7 Input: root = [3,9,20,null,null,15,7] Output: [ [15,7],[9,20],[3] ] ...

Symmetric Binary Tree Problem

Symmetric Tree Problem Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). Examples Example 1: ...

Construct String from Binary Tree Problem

Problem Given the root of a binary tree, construct a string consisting of parenthesis and integers from a binary tree with the preorder traversal way, and return it. Omit all the empty parenthesis pairs that do not affect the one-to-one mapping relationship between the string and the original binary tree. Examples Example 1: 1 / \ 2 3 / 4 ...

Morris Traversal

Can we perform inorder traversal on a binary tree without using recursion or a stack, while also keeping the space complexity constant? This is where Morris traversal comes in. We have seen inorder traversal of binary tree here - Binary Tree Traversal - Inorder. The iterative version involves stack. Now lets see Morris Traversal. The idea of Morris Traversal is based on Threaded Binary Tree. This approach achieves inorder traversal by establishing temporary links within the tree itself. The data is then printed by following these links, and finally, the tree structure is restored to its original state. ...

Path Sum Problem

Problem Given the root of a binary tree and an integer targetSum, return true if the tree has a root-to-leaf path such that adding up all the values along the path equals targetSum. A leaf is a node with no children. Examples Example 1: graph TD; A[5] --> B[4] & C[8] B --> D[11] & E[null] C --> F[13] & G[4] D --> H[7] & I[2] G --> L[5] & M[1] style A fill:#f9f style B fill:#f9f style D fill:#f9f style I fill:#f9f ...

Lowest Common Ancestor Definition

Definition According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).” Examples graph TD; 6; 6 --- 2; 6 --- 8; 2 --- 0; 2 --- 4; 8 --- 7; 8 --- 9; 4 --- 3; 4 --- 5; ...

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