Delete Nodes And Return Forest Problem

Problem Given the root of a binary tree, each node in the tree has a distinct value. After deleting all nodes with a value in to_delete, we are left with a forest (a disjoint union of trees). Return the roots of the trees in the remaining forest. You may return the result in any order. Examples Example 1: graph TD; 1; 1 --- 2; 1 --- 3; 2 --- 4; 2 --- 5; 3 --- 6; 3 --- 7; ...

Locking and unlocking resources represented as binary tree nodes

Problem Implement locking in a binary tree. A binary tree node can be locked or unlocked only if all of its descendants or ancestors are not locked. Design a binary tree node class with the following methods: is_locked, which returns whether the node is locked lock, which attempts to lock the node. If it cannot be locked, then it should return false. Otherwise, it should lock it and return true. unlock, which unlocks the node. If it cannot be unlocked, then it should return false. Otherwise, it should unlock it and return true. You may augment the node to add parent pointers or any other property you would like. You may assume the class is used in a single-threaded program, so there is no need for actual locks or mutexes. Each method should run in O(h), where h is the height of the tree. ...

Minimum Level Sum of a Binary Tree Problem

Problem Given a binary tree, return the level of the tree with minimum sum. OR Given the root of a binary tree, the level of its root is 1, the level of its children is 2, and so on. Return the smallest level x such that the sum of all the values of nodes at level x is minimal. Opposite of this problem - Minimum Level Sum of a Binary Tree Problem. ...

Distribute Coins in Binary Tree Problem

Problem You are given the root of a binary tree with n nodes where each node in the tree has node.val coins. There are n coins in total throughout the whole tree. In one move, we may choose two adjacent nodes and move one coin from one node to another. A move may be from parent to child, or from child to parent. Return the minimum number of moves required to make every node have exactly one coin. ...

Delete Leaves With a Given Value Problem

Problem Given a binary tree root and an integer target, delete all the leaf nodes with value target. Note that once you delete a leaf node with value target, if its parent node becomes a leaf node and has the value target, it should also be deleted (you need to continue doing that until you cannot). Examples Example 1: ...

Given a binary search tree with 2 nodes swapped find number of pairs not following BST properties

Given a binary search tree with 2 nodes swapped find number of pairs not following BST properties Problem Given a binary search tree with 2 nodes swapped find number of pairs not following BST properties Follow up: Recover Binary Search Tree Problem Examples Example 1 Consider the BST: 10 / \ 5 (8) / \ 2 (20) ...

Binary Tree Traversal - Zigzag Level Order Traversal

Problem Given the root of a binary tree, return the zigzag level order traversal of its nodes’ values. (i.e., from left to right, then right to left for the next level and alternate between). Example Example 1 Tree Traversal Direction 3 -> / \ 9 20 <- / \ 15 7 -> ...

Construct Binary Tree from Inorder and Postorder Traversal

Problem Given two integer arrays inorder and postorder where inorder is the inorder traversal of a binary tree and postorder is the postorder traversal of the same tree, construct and return the binary tree. Example Example 1: 3 / \ 9 20 / \ 15 7 ...

Construct Binary Tree from Inorder and Preorder Traversal

Problem Given two integer arrays preorder and inorder where preorder is the preorder traversal of a binary tree and inorder is the inorder traversal of the same tree, construct and return the binary tree. Examples Example 1: 3 / \ 9 20 / \ 15 7 ...

Depth First Search

Depth First Search DFS Traversal for Graph The idea Depth-First Search (DFS) is a graph traversal algorithm that explores as deeply as possible along each branch before backtracking to explore other options. This is similar to how we play a maze game. DFS can be implemented iteratively using a stack data structure, similar to Breadth-First Search (BFS) which utilizes a queue. Alternatively, a recursive approach is very natural for DFS due to its depth-oriented exploration. We will explore both implementations in detail later. ...

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