Given a binary tree, find its height.
Example 1:
Input:
     1
    /  \
   2    3
Output: 2
Example 2:
Input:
  2
   \
    1
   /
 3
Output: 3   
Your Task:
You don't need to read input or print anything. Your task is to complete the function height() which takes root node of the tree as input parameter and returns an integer denoting the height of the tree. If the tree is empty, return 0. 
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 <= Number of nodes <= 10^{5}
1 <= Data of a node <= 10^{5}

Source: geeksforgeeks | Difficulty: EASY
Tags: Tree algorithms

Data structures

Starter code: #User function Template for python3

'''
# Node Class:
class Node:
    def _init_(self,val):
        self.data = val
        self.left = None
        self.right = None
        '''
class Solution:
    #Function to find the height of a binary tree.
    def height(self, root):
        # code here


Solutions:
class Solution:

	def height(self, root):
		if root is None:
			return 0
		return 1 + max(self.height(root.left), self.height(root.right))


class Solution:

	def height(self, root):
		if root == None:
			return 0
		l = self.height(root.left)
		r = self.height(root.right)
		if l > r:
			return l + 1
		else:
			return r + 1


class Solution:

	def height(self, root):
		if root == None:
			return 0
		lt = self.height(root.left)
		rt = self.height(root.right)
		return max(lt, rt) + 1


class Solution:

	def height(self, root):
		if not root:
			return 0
		l_height = self.height(root.left)
		r_height = self.height(root.right)
		return max(l_height, r_height) + 1


class Solution:

	def height(self, root):
		if root == None:
			return 0
		left_root = self.height(root.left)
		right_root = self.height(root.right)
		return 1 + max(left_root, right_root)


class Solution:

	def height(self, root):
		if root == None:
			return 0
		lh = self.height(root.left)
		rh = self.height(root.right)
		ans = max(lh, rh) + 1
		return ans


class Solution:

	def height(self, root):
		if root == None:
			return 0
		if not root.left and (not root.right):
			return 1
		if not root.right:
			return self.height(root.left) + 1
		if not root.left:
			return self.height(root.right) + 1
		rightDepth = self.height(root.right)
		leftDepth = self.height(root.left)
		return max(rightDepth, leftDepth) + 1


class Solution:

	def height(self, root):
		if root is None:
			return 0
		else:
			ldepth = self.height(root.left)
			rdepth = self.height(root.right)
			if ldepth > rdepth:
				return ldepth + 1
			return rdepth + 1


from collections import deque

class Solution:

	def height(self, node, depth=1):
		left_depth = right_depth = depth
		if node.left:
			left_depth = self.height(node.left, depth + 1)
		if node.right:
			right_depth = self.height(node.right, depth + 1)
		return max(left_depth, right_depth)


from collections import deque

class Solution:

	def height(self, node):
		queue = deque()
		queue.append(node)
		counter = 0
		while queue:
			len_of_queue = len(queue)
			counter += 1
			for i in range(len_of_queue):
				temp = queue.popleft()
				if temp.left:
					queue.append(temp.left)
				if temp.right:
					queue.append(temp.right)
		return counter


class Solution:

	def height(self, root):
		if root == None:
			return 0
		x = Solution.height(self, root.left)
		y = Solution.height(self, root.right)
		return max(x, y) + 1


class Solution:

	def height(self, root):
		if root is None:
			return 0
		else:
			left_height = self.height(root.left)
			right_height = self.height(root.right)
			return max(left_height, right_height) + 1


class Solution:

	def height(self, root):
		h = 0
		if root is None:
			return 0
		lheight = self.height(root.left) + 1
		rheight = self.height(root.right) + 1
		return max(lheight, rheight)


from collections import deque

class Solution:

	def height(self, root):
		q = deque([root])
		ans = 0
		while q:
			for _ in range(len(q)):
				x = q.popleft()
				if x.left:
					q.append(x.left)
				if x.right:
					q.append(x.right)
			ans += 1
		return ans


class Solution:

	def height(self, root):
		if root is None:
			return 0
		else:
			left_ht = self.height(root.left)
			right_ht = self.height(root.right)
			return max(left_ht, right_ht) + 1


class Solution:

	def height(self, root):
		if root == None:
			return 0
		else:
			lht = self.height(root.left)
			rht = self.height(root.right)
			if lht > rht:
				return lht + 1
			else:
				return rht + 1


class Solution:

	def height(self, node):
		if node is None:
			return 0
		else:
			lDepth = self.height(node.left)
			rDepth = self.height(node.right)
			if lDepth > rDepth:
				return lDepth + 1
			else:
				return rDepth + 1


class Solution:

	def height(self, root):
		if root is None:
			return 0
		else:
			lheight = self.height(root.left)
			rheight = self.height(root.right)
			if lheight > rheight:
				return lheight + 1
			else:
				return rheight + 1


class Solution:

	def height(self, root):
		if not root:
			return 0
		lh = Solution.height(self, root.left)
		rh = Solution.height(self, root.right)
		return 1 + max(lh, rh)


class Solution:

	def height(self, root):
		if not root:
			return 0
		l = self.height(root.left)
		r = self.height(root.right)
		return max(l, r) + 1


class Solution:

	def height(self, root):
		t = root
		height = 1
		ans = []

		def traverse(t, height, ans):
			ans.append(height)
			if t is not None:
				if t.left is not None:
					traverse(t.left, height + 1, ans)
				if t.right is not None:
					traverse(t.right, height + 1, ans)
		traverse(t, height, ans)
		return max(ans)


class Solution:

	def height(self, root):
		if not root:
			return 0
		leftChild = self.height(root.left)
		rightChild = self.height(root.right)
		return 1 + max(leftChild, rightChild)


class Solution:

	def height(self, root):
		if root is not None:
			l_h = self.height(root.left)
			r_h = self.height(root.right)
			return max(l_h, r_h) + 1
		else:
			return 0


class Solution:

	def height(self, root):
		if root is None:
			return 0
		l = self.height(root.left)
		r = self.height(root.right)
		ans = max(l, r) + 1
		return ans


class Solution:

	def height(self, root):
		if root is None:
			return 0
		lh = self.height(root.left)
		rh = self.height(root.right)
		if root.left and root.right:
			return 1 + max(lh, rh)
		elif root.left:
			return 1 + lh
		else:
			return 1 + rh


import math

class Solution:

	def solve(self, root):
		if root is None:
			return 0
		return 1 + max(self.solve(root.left), self.solve(root.right))

	def height(self, root):
		return self.solve(root)


class Solution:

	def height(self, root):
		if not root:
			return 0
		l = 1
		s = [[root, 1]]
		while s:
			(n, d) = s.pop()
			if n:
				s.append([n.left, d + 1])
				s.append([n.right, d + 1])
				l = max(l, d)
		return l


class Solution:

	def height(self, root):
		if not root:
			return 0
		queue = [[root]]
		height = 1
		while queue:
			level = queue.pop(0)
			temp = []
			for node in level:
				if node.left:
					temp.append(node.left)
				if node.right:
					temp.append(node.right)
			if temp:
				queue.append(temp)
				height += 1
		return height


class Solution:

	def _init_(self, val):
		self.data = val
		self.left = None
		self.right = None

	def height(self, root):
		if root == None:
			return 0
		else:
			return max(self.height(root.left), self.height(root.right)) + 1


class Solution:

	def height(self, root):
		h = self.solve(root)
		return h

	def solve(self, root):
		if root == None:
			return 0
		return max(self.height(root.left), self.height(root.right)) + 1


class Solution:

	def height(self, root):

		def tree(node):
			if not node:
				return 0
			return 1 + max(tree(node.right), tree(node.left))
		return tree(root)


class Solution:

	def height(self, root):
		if not root:
			return 0
		(ldp, rdp) = (0, 0)
		ldp = self.height(root.left)
		rdp = self.height(root.right)
		if ldp > rdp:
			return ldp + 1
		else:
			return rdp + 1


class Solution:

	def height(self, root):
		if root:
			return 1 + max(self.height(root.left), self.height(root.right))
		else:
			return int(0)


class Solution:

	def height(self, root):
		stack = []
		if root == None:
			return 0
		if root.left == None and root.right == None:
			return 1
		if root.left == None:
			return 1 + self.height(root.right)
		if root.right == None:
			return 1 + self.height(root.left)
		return 1 + max(self.height(root.left), self.height(root.right))


class Solution:

	def height(self, root):
		result = []
		self.leftView(root, result, 0)
		return len(result)

	def leftView(self, node, result, level):
		if not node:
			return
		if level == len(result):
			result.append(node.data)
		self.leftView(node.left, result, level + 1)
		self.leftView(node.right, result, level + 1)


k = 0

def recur(root, u):
	global k
	if root == None:
		if u > k:
			k = u
		return
	recur(root.left, u + 1)
	recur(root.right, u + 1)

class Solution:

	def height(self, root):
		global k
		k = 0
		recur(root, 0)
		return k


class Solution:

	def height(self, root):

		def h(node):
			if node == None:
				return 0
			rh = h(node.right)
			lh = h(node.left)
			return max(rh, lh) + 1
		return h(root)


class Solution:

	def height(self, root):
		if root == None:
			return 0
		countleft = self.height(root.left)
		countright = self.height(root.right)
		ans = max(countleft, countright) + 1
		return ans


class Solution:

	def height(self, root):
		if root == None:
			return 0
		h1 = self.height(root.left)
		h2 = self.height(root.right)
		return 1 + max(h1, h2)


class Solution:

	def height(self, root):
		if root == None:
			return 0
		else:
			ls = self.height(root.left)
			rs = self.height(root.right)
			return max(ls, rs) + 1


class Solution:

	def height(self, root):
		queue = [root]
		answer = 0
		while queue:
			size = len(queue)
			answer += 1
			for i in range(size):
				node = queue.pop(0)
				if node.left:
					queue.append(node.left)
				if node.right:
					queue.append(node.right)
		return answer


class Solution:

	def height(self, root):
		maxh = 0

		def inOrder(root, h):
			nonlocal maxh
			if root:
				if h > maxh:
					maxh = h
				inOrder(root.left, h + 1)
				inOrder(root.right, h + 1)
		inOrder(root, 1)
		return maxh


class Solution:

	def height(self, root):
		if root == None:
			return 0
		if root.left == None and root.right == None:
			return 1
		smallleft = self.height(root.left)
		smallright = self.height(root.right)
		return max(smallleft, smallright) + 1