Skip to content

Commit 4ae3a68

Browse files
authored
Create binary_tree_upside_down.py
1 parent ea69035 commit 4ae3a68

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

binary_tree_upside_down.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""
2+
Definition of TreeNode:
3+
class TreeNode:
4+
def __init__(self, val):
5+
self.val = val
6+
self.left, self.right = None, None
7+
"""
8+
9+
class Solution:
10+
"""
11+
@param root: the root of binary tree
12+
@return: new root
13+
"""
14+
def upsideDownBinaryTree(self, root):
15+
# write your code here
16+
# 递归版本,非递归待版本TODO。
17+
if (root == None) or (root.left == None):
18+
return root
19+
_root = self.upsideDownBinaryTree(root.left)
20+
root.left.left = root.right
21+
root.left.right = root
22+
root.left = None
23+
root.right = None
24+
return _root
25+
26+
# medium: https://www.lintcode.com/problem/binary-tree-upside-down

0 commit comments

Comments
 (0)