Lowest Common Ancestor of a Binary Tree 3 - Given Parent Pointer

Problem Given values of two nodes in a Binary Tree p and q, find the Lowest Common Ancestor (LCA). It may be assumed that both nodes exist in the tree and Node has parent pointer. class Node { public int val; public Node left; public Node right; public Node parent; } ...

Lowest Common Ancestor for a n-ary Tree

Lowest Common Ancestor LCA in N-ary Tree Problem In a company which has CEO Bill and a hierarchy of employees. Employees can have a list of other employees reporting to them, which can themselves have reports, and so on. An employee with at least one report is called a manager. Please implement the closestCommonManager method to find the closest manager (i.e. farthest from the CEO) to two employees. You may assume that all employees eventually report up to the CEO. Assume the following class which you can’t change – ...

Invert Binary Tree Problem

Problem Given the root of a binary tree, invert the tree, and return its root. OR Change a tree so that the roles of the left and right pointers are swapped at every node. Example Example 1 Given binary tree 1 / \ 2 3 / \ / \ 4 5 6 7 ...

Merge Two Binary Trees

Problem You are given two binary trees root1 and root2. Imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not. You need to merge the two trees into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as the node of the new tree. ...

Deepest Left Node in a Binary Tree

Problem Given a binary tree, Find the deepest left node in it. Examples Example 1: 1 / \ 2 3 / \ 4 7 ...

August 8, 2022 · 1 min · TagsList of tags for the post  bt-binary-tree

Binary Tree Cameras Problem

Binary Tree Cameras Problem Problem You are given the root of a binary tree. We install cameras on the tree nodes where each camera at a node can monitor its parent, itself, and its immediate children. Return the minimum number of cameras needed to monitor all nodes of the tree. Examples Example 1: ...

Binary Tree Traversal - Vertical Order Traversal

Problem Given a binary tree, return the vertical order traversal of its nodes’ values. (ie, from top to bottom, column by column). Examples Example 1: ...

Find right-most cousin in a binary tree

Problem Given a node in a binary tree. Find the rightmost cousin of the give node. Examples Example 1 Let’s start with an example. 1 / \ 2 3 / / \ 4 5 6 \ / 7 8 ...

Flatten Binary Tree to Linked List in Order of Preorder Traversal

Problem Given the root of a binary tree, flatten the tree into a “linked list”: The “linked list” should use the same TreeNode class where the right child pointer points to the next node in the list and the left child pointer is always null. The “linked list” should be in the same order as a pre-order traversal of the binary tree. Examples 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