edit metadata

The height of a binary tree is the number of edges between the tree's root and its furthest leaf. This means that a tree containing a single node has a height of.

Complete the_getHeight_function provided in your editor so that it returns the height of a binary tree

Input Format

You do not need to read any input from stdin. Our grader will pass the root node of a binary tree to your_getHeight_function.

Output Format

Your function should return a single integer denoting the height of the binary tree.

Sample Input

Note:A_binary search tree_is a binary tree in which the value of each parent node's left child is less than the value the parent node, and the value of the parent node is less than the value of its right child.

Sample Output

3

Code:

def height(root):
    if root is None:
        return None


    q = []
    q.append([root,0])
    temp = 0
    while len(q)!=0:
        node,depth = q.pop()
        temp = max(temp,depth)
        if node.left is not None:
            q.append([node.left,depth+1])
        if node.right is not None:
            q.append([node.right,depth+1])
    return temp

results matching ""

    No results matching ""