LeetCode 2415 | Reverse Odd Levels of Binary Tree | Python Solution

 LeetCode Problem Link : https://leetcode.com/problems/reverse-odd-levels-of-binary-tree/description/


Video Solution : https://www.youtube.com/watch?v=oidCQx8j7GE


# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):
    def reverseOddLevels(self, root):
        """
        :type root: Optional[TreeNode]
        :rtype: Optional[TreeNode]
        """
        def helper(root1,root2, depth):
            if root1 is None and root2 is None:
                return None

            if depth % 2 != 0:
                 tmp = root1.val
                 root1.val = root2.val
                 root2.val = tmp

            helper(root1.left,root2.right, depth +1 )
            helper(root1.right,root2.left, depth +1 )

        helper(root.left , root.right , 1)  
        return root 

Comments

Popular posts from this blog

JDBC Hive Connection fails : Unable to read HiveServer2 uri from ZooKeeper

Access Kubernetes ConfigMap in Spring Boot Application

Developing Custom Processor in Apache Nifi