1. What I learned
a. A function in another function
We can define a different function within a function.
def fuctionA(self,ex):
def functionB(self,pp):
#There's no error
b. LIST.pop(0)
To remove the first element in a list, write as follows.
LIST = [1,2,3]
LIST.pop(0)
print LIST
#It will print '2,3'
2. How I sloved
I used DFS to arrange the elements in ascending order. Then I made a BST using the sorted list. That BST stretched only to the right.
3. Code
class Solution:
def increasingBST(self, root: TreeNode) -> TreeNode:
def dfs(root):
if root is None:
pass
else:
dfs(root.left)
elements.append(root.data)
dfs(root.right)
def createBST(elist):
new_root = TreeNode(elist[0])
cur_root = new_root
elist.pop(0)
for e in elist:
cur_root.right = TreeNode(e)
cur_root = cur_root.right
return new_root
elements = []
dfs(root)
return createBST(elements)
4. Result
Runtime : 24 ms(96.34%), Memory usage : 14.2 MB(5.14%)
(Runtime can be different by a system even if it is a same code.)