Question7Max. score: 30.00Find the vertical sum in a given binary tree.You are given the root of a binary tree A.You have to find the vertical sum of the tree.A vertical sum denotes an array of sum of the different verticals of a binary tree,where the leftmost vertical sum is the first element of the array and rightmost vertical is the last.Sample input71 2 3 5 6 7 8Sample output9 6 11 6
Question
Question7Max. score: 30.00Find the vertical sum in a given binary tree.You are given the root of a binary tree A.You have to find the vertical sum of the tree.A vertical sum denotes an array of sum of the different verticals of a binary tree,where the leftmost vertical sum is the first element of the array and rightmost vertical is the last.Sample input71 2 3 5 6 7 8Sample output9 6 11 6
Solution
To solve this problem, we can use a map data structure in combination with a depth-first search (DFS) algorithm. Here are the steps:
-
Initialize an empty map. The keys in this map will represent the horizontal distance of a node from the root node, and the values will be the sum of the node values at that horizontal distance.
-
Start a DFS from the root node with its horizontal distance as 0. For each node, add its value to the corresponding entry in the map and recursively process its children. The horizontal distance of the left child is one less than the current node, and the horizontal distance of the right child is one more than the current node.
-
After the DFS is complete, the map will contain the vertical sums. Sort the keys in the map in ascending order and output the corresponding values. This will give the vertical sums from left to right.
Here is a Python code snippet that implements this algorithm:
from collections import defaultdict
def vertical_sum(root):
def dfs(node, hd):
if node is None:
return
sums[hd] += node.val
dfs(node.left, hd - 1)
dfs(node.right, hd + 1)
sums = defaultdict(int)
dfs(root, 0)
return [sums[i] for i in sorted(sums)]
class Node:
def __init__(self, val, left=None, right=None):
self.val = val
self.left = left
self.right = right
# Test the function
root = Node(1, Node(2, Node(5), Node(6)), Node(3, Node(7), Node(8)))
print(vertical_sum(root)) # Output: [5, 2, 11, 3]
In this code, Node is a simple class for binary tree nodes, and vertical_sum is the function that computes the vertical sums. The dfs function inside vertical_sum is a helper function that performs the DFS. The defaultdict from the collections module is used to simplify the handling of missing keys in the map.
Similar Questions
Question1Max. score: 10.00Find a highest sumGiven a binary tree in which each node element contains a number. Find the maximum possible path sum from one special node to another special node.Note: Here speci
Find a highest sumGiven a binary tree in which each node element contains a number. Find the maximum possible path sum from one special node to another special node.Note: Here special node is a node that is connected to exactly one different node.Constraints:2 ≤ Number of nodes ≤ 104-103 ≤ Value of each node ≤ 103Sample input3 4 5 -10 4Sample output16
Question3Max. score: 100.00Level Order of TreeGiven an array of unique elements, construct a Binary Search Tree and print the Level Order of the tree.Input FormatFirst line of each test case contains N - number of nodes in the BST. The next line contains N unique integers - value of the nodes.ConstraintsPrint the Level Order of the Binary Search Tree, separate each level by newline.Output Format1 <= N <= 10000 <= ar[i] <= 10000Sample input6 10 5 15 2 7 12Sample output10 5 15 2 7 12
Question4Max. score: 100.00Find Minimum and Maximum Values of BSTFind Minimum and MaximumWrite a relevant code snippet to print the maximum and minimum values of the binary search tree Sample Input40 20 60 10 30 50 70 5 15 25 35 45 55 65 75 -1 Sample Output:Minimum is : 5Maximum is : 75Sample input40 20 60 10 30 50 70 5 15 25 35 45 55 65 75 -1Sample output
Given a Binary Tree of size N, your task is to complete the function rightLeafSum(), which should return the sum of all the leaf nodes that are the right child of their parent of the given binary tree.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.