Skip to content

Instantly share code, notes, and snippets.

@arpi-r
Created March 18, 2019 03:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arpi-r/16b9250cddc73cc5659725480ac39225 to your computer and use it in GitHub Desktop.
Save arpi-r/16b9250cddc73cc5659725480ac39225 to your computer and use it in GitHub Desktop.
codebuddy week12
# Definition for a binary tree node
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
# @param A : root node of tree
# @return the root node in the tree
def getpreorder(self,x,l):
if x == None:
return l
l.append(x.val)
l = self.getpreorder(x.left,l)
l = self.getpreorder(x.right,l)
return l
def flatten(self, A):
l = []
r = self.getpreorder(A,l)
root = TreeNode(l[0])
prev = root
for i in range(1,len(l)):
x = TreeNode(l[i])
prev.right = x
prev = x
return root
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment