forked from Rishu1018/HackertoberFest2022-Repostory_Newbie
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Ashish93-mrx/Ashish93-mrx-patch-1
Added code to get height of tree using recurssion in python
- Loading branch information
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#this program is use to get the height of the binar tree using recurrsion | ||
|
||
#Time and Space Complexity: | ||
#The time complexity of the algorithm is O(n) as we iterate through node of the binary tree calculating the height of the binary tree only once. | ||
#And the space complexity is also O(n) as we are following recursion, where recursive stack can have upto n elements. | ||
#Where n is the number of nodes in the binary tree. | ||
|
||
#define a Class Tree, to intiate the binary tree | ||
class TreeNode: | ||
def __init__(self, val): | ||
self.val = val | ||
self.left = None | ||
self.right = None | ||
|
||
def height(root): | ||
|
||
# Check if the binary tree is empty | ||
if root is None: | ||
# If TRUE return 0 | ||
return 0 | ||
# Recursively call height of each node | ||
leftAns = height(root.left) | ||
rightAns = height(root.right) | ||
|
||
# Return max(leftHeight, rightHeight) at each iteration | ||
return max(leftAns, rightAns) + 1 | ||
|
||
# Test the algorithm | ||
root = TreeNode(1) | ||
root.left = TreeNode(2) | ||
root.right = TreeNode(3) | ||
root.left.left = TreeNode(4) | ||
|
||
print("Height of the binary tree is: " + str(height(root))) |