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; ...

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: ...

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 ...

Sum of Distances in Tree Problem

Sum of Distances in Tree Problem Problem There is an undirected connected tree with n nodes labeled from 0 to n - 1 and n - 1 edges. You are given the integer n and the array edges where edges[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the tree. Return an array answer of length n where answer[i] is the sum of the distances between the ith node in the tree and all other nodes. ...

Number of Good Leaf Nodes Pairs Problem

Problem You are given the root of a binary tree and an integer distance. A pair of two different leaf nodes of a binary tree is said to be good if the length of the shortest path between them is less than or equal to distance. Return the number of good leaf node pairs in the tree. Examples Example 1: 1 / \ 2 3 \ 4 ...

Lowest Common Ancestor of a Binary Tree

Problem Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. Definition Lowest Common Ancestor Definition Examples Example 1: graph TD; 6; 6 --- 2; 6 --- 8; 2 --- 0; 2 --- 4; 8 --- 7; 8 --- 9; 4 --- 3; 4 --- 5; style 2 fill:#FF9933 style 8 fill:#FF9933 style 6 fill:#3399FF ...

Binary Tree Postorder Traversal

Problem Given a binary tree, write an algorithm for postorder traversal. Postorder Traversal Visit all the nodes in the left subtree Visit all the nodes in the right subtree Visit the root node postorder(root->left) postorder(root->right) display(root->data) Examples Example 1: 1 \ 2 / 3 ...

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 ...

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 ...

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