
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to find sum each of the diagonal path elements in a binary tree in Python
Suppose we have a binary tree, we have to find the sum of each of the diagonals in the tree starting from the top to bottom right.
So, if the input is like
then the output will be [27, 18, 3] as the diagonals are [12,15], [8,10],[3]. So the sum values are [27, 18, 3]
To solve this, we will follow these steps −
Define a function traverse() . This will take node, numLeft, output
if node is null, then
return
if numLeft >= size of output , then
insert data of node at the end of output
otherwise,
output[numLeft] := output[numLeft] + data of node
if left of node is not null, then
traverse(left of node, numLeft+1, output)
if right of node is not null, then
traverse(right of node, numLeft, output)
From the main method, do the following −
output := a new list
traverse(root, 0, output)
return output
Let us see the following implementation to get better understanding −
Example
class TreeNode: def __init__(self, data, left = None, right = None): self.data = data self.left = left self.right = right class Solution: def solve(self, root): output = [] def traverse(node, numLeft, output): if not node: return if numLeft >= len(output): output.append(node.data) else: output[numLeft] += node.data if node.left: traverse(node.left, numLeft+1, output) if node.right: traverse(node.right, numLeft, output) traverse(root, 0, output) return output ob = Solution() root = TreeNode(12) root.left = TreeNode(8) root.right = TreeNode(15) root.left.left = TreeNode(3) root.left.right = TreeNode(10) print(ob.solve(root))
Input
root = TreeNode(12) root.left = TreeNode(8) root.right = TreeNode(15) root.left.left = TreeNode(3) root.left.right = TreeNode(10)
Output
[27, 18, 3]
- Related Articles
- Program to find largest sum of any path of a binary tree in Python
- Program to find sum of longest sum path from root to leaf of a binary tree in Python
- Program to find sum of all numbers formed by path of a binary tree in python
- Program to find the largest sum of the path between two nodes in a binary tree in Python
- Binary Tree Maximum Path Sum in Python
- Diagonal Sum of a Binary Tree in C++?
- Program to find longest even value path of a binary tree in Python
- Program to find length of longest alternating path of a binary tree in python
- Program to find length of longest consecutive path of a binary tree in python
- Python Program to Find the Sum of All Nodes in a Binary Tree
- Program to find sum of all elements of a tree in Python
- Program to find most frequent subtree sum of a binary tree in Python
- Maximum Path Sum in a Binary Tree in C++
- Program to find diagonal sum of a matrix in Python
- Python Program To Find the Smallest and Largest Elements in the Binary Search Tree
