0% found this document useful (0 votes)
12 views

Problem Statement On Binary Tree

Binary

Uploaded by

rahilinamdar92
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Problem Statement On Binary Tree

Binary

Uploaded by

rahilinamdar92
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Problem statement 1:

You are given the root of a binary tree that consists of exactly 3 nodes:
the root, its left child, and its right child.

Return true if the value of the root is equal to the sum of the values of its
two children, or false otherwise.

Example 1:

Input: root = [10,4,6]


Output: true
Explanation: The values of the root, its left child, and its right child are
10, 4, and 6, respectively.
10 is equal to 4 + 6, so we return true.
Example 2:

Input: root = [5,3,1]


Output: false
Explanation: The values of the root, its left child, and its right child are
5, 3, and 1, respectively.
5 is not equal to 3 + 1, so we return false.

Constraints:
The tree consists only of the root, its left child, and its right child.
-100 <= Node.val <= 100
__________________________________________________________
Problem Statement 2:

Given the root of a binary tree, return the preorder traversal of its nodes'
values.

Example 1:
Input: root = [1,null,2,3]
Output: [1,2,3]

Example 2:
Input: root = []
Output: []

Example 3:
Input: root = [1]
Output: [1]

Constraints:

The number of nodes in the tree is in the range [0, 100].


-100 <= Node.val <= 100
Follow up: Recursive solution is trivial, could you do it iteratively?

__________________________________________________________
Problem Statement 3:

You are given the root of a full binary tree with the following properties:

Leaf nodes have either the value 0 or 1, where 0 represents False and 1
represents True.
Non-leaf nodes have either the value 2 or 3, where 2 represents the
boolean OR and 3 represents the boolean AND.
The evaluation of a node is as follows:

If the node is a leaf node, the evaluation is the value of the node, i.e.
True or False.
Otherwise, evaluate the node's two children and apply the boolean
operation of its value with the children's evaluations.
Return the boolean result of evaluating the root node.

A full binary tree is a binary tree where each node has either 0 or 2
children.

A leaf node is a node that has zero children.

Example 1:

Input: root = [2,1,3,null,null,0,1]


Output: true
Explanation: The above diagram illustrates the evaluation process.
The AND node evaluates to False AND True = False.
The OR node evaluates to True OR False = True.
The root node evaluates to True, so we return true.
Example 2:

Input: root = [0]


Output: false
Explanation: The root node is a leaf node and it evaluates to false, so we
return false.

Constraints:

The number of nodes in the tree is in the range [1, 1000].


0 <= Node.val <= 3
Every node has either 0 or 2 children.
Leaf nodes have a value of 0 or 1.
Non-leaf nodes have a value of 2 or 3.
_________________________________________________________

Problem Statement 4:

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.

Return the merged tree.

Note: The merging process must start from the root nodes of both trees.

Example 1:

Input: root1 = [1,3,2,5], root2 = [2,1,3,null,4,null,7]


Output: [3,4,5,5,4,null,7]
Example 2:

Input: root1 = [1], root2 = [1,2]


Output: [2,2]

Constraints:

The number of nodes in both trees is in the range [0, 2000].


-104 <= Node.val <= 104
_________________________________________________________

Problem Statement 5:

Given the root of a binary tree, invert the tree, and return its root.

Example 1:

Input: root = [4,2,7,1,3,6,9]


Output: [4,7,2,9,6,3,1]
Example 2:

Input: root = [2,1,3]


Output: [2,3,1]
Example 3:

Input: root = []
Output: []
Constraints:

The number of nodes in the tree is in the range [0, 100].


-100 <= Node.val <= 100
__________________________________________________________

Problem Statement 6:
Given the root of a binary tree, return the inorder traversal of its nodes'
values.

Example 1:
Input: root = [1,null,2,3]
Output: [1,3,2]
Example 2:

Input: root = []
Output: []
Example 3:

Input: root = [1]


Output: [1]

Constraints:

The number of nodes in the tree is in the range [0, 100].


-100 <= Node.val <= 100

Follow up: Recursive solution is trivial, could you do it iteratively?

You might also like