Invert Binary Tree in Python

My Projects
Change Password
My Profile
Logout
Undo
Redo
Cut
Copy
Paste
Delete
Select All
Find
Find and Replace
Editor Theme
Crimson
Eclipse
Github
Solarized
Cobalt
krTheme
Monokai
Terminal
Textmate
Twilight
Vibrant Ink
Font Size
8px
9px
10px
11px
12px
13px
14px
15px
16px
17px
18px
20px
22px
24px
Tab Size
1
2
3
4
5
6
7
8
Show Invisible
Hide Invisible
Show Line Numbers
Hide Line Numbers
Ace Editor (Default)
Vim Editor
Emacs Editor
Open New Project
Save Project
Save As New Project
Share Project
Search Project
class TreeNode: def __init__(self, data, left = None, right = None): self.data = data self.left = left self.right = right def make_tree(elements): Tree = TreeNode(elements[0]) for element in elements[1:]: insert(Tree, element) return Tree def height(root): if root is None: return 0 else : # Compute the height of left and right subtree l_height = height(root.left) r_height = height(root.right) #Find the greater one, and return it if l_height > r_height : return l_height+1 else: return r_height+1 def print_given_level(root, level): if root is None: return if level == 1: print(root.data,end = ',') elif level > 1 : print_given_level(root.left , level-1) print_given_level(root.right , level-1) def level_order(root): h = height(root) for i in range(1, h+1): print_given_level(root, i) def insert(temp,data): que = [] que.append(temp) while (len(que)): temp = que[0] que.pop(0) if (not temp.left): if data is not None: temp.left = TreeNode(data) else: temp.left = TreeNode(0) break else: que.append(temp.left) if (not temp.right): if data is not None: temp.right = TreeNode(data) else: temp.right = TreeNode(0) break else: que.append(temp.right) class Solution(object): def invertTree(self, root): """ :type root: TreeNode :rtype: TreeNode """ self.solve(root) return root def solve(self,root): if not root: return temp = root.left root.left = root.right root.right = temp self.solve(root.left) self.solve(root.right) tree1 = make_tree([1,2,2,3,4,None,3]) ob1 = Solution() tree2 = ob1.invertTree(tree1) level_order(tree2)

Invert Binary Tree in Python (Python v3.6.2)

Invert Binary Tree in Python (Python v3.6.2) helps you to Edit, Run and Share your Python3 Code directly from your browser. This development environment provides you version Python v3.6.2.

How to give program Input?

The latest version of Coding Ground allows to provide program input at run time from the termnial window exactly the same way as you run your program at your own computer. So simply run a program and provide your program input (if any) from the terminal window available in the right side.

Keyboard Shortcuts

ShortcutDescription
⌘ + EnterRun the program
⌘ + SSave Project (Login Required)
⇧ + ⌘ + SSave As Project
⌘ + PNew Project
⌘ + GShare Project
⌘ + ZUndo Editing
⌘ + YRedo Editing
⌘ + ASelect All Text
⌘ + XCut Selected Text
⌘ + CCopy Selected Text
⌘ + VPaste Copied Text
⌘ + FSearch Text
⌘ + ⌥ + FReplace Text
ShortcutDescription
Ctrl + EnterRun the program
Ctrl + SSave Project
Shift + Ctrl + SSave As Project
Ctrl + GShare Project
Ctrl + ZUndo Editing
Ctrl + YRedo Editing
Ctrl + ASelect All Text
Ctrl + XCut Selected Text
Ctrl + CCopy Selected Text
Ctrl + VPaste Copied Text
Ctrl + FSearch Text
Ctrl + HReplace Text

Save Python3 Project

You can save your Python3 Project with us so that you can access this project later on. To save a project you will need to create a login Id with us. So before you save a project, please create a login Id using a link given at the top right corner of this page.

Share Python3 Project

You can use this feature to share your Python3 Code with your teachers, classmates and colleagues. Just click Share Button and it will create a short link, which can be shared through Email, WhatsApp or even through Social Media. A shared link will be deleted if it has been passive for almost 3 months.

 Execute |  Beautify | Share