Created
March 18, 2019 03:37
-
-
Save arpi-r/16b9250cddc73cc5659725480ac39225 to your computer and use it in GitHub Desktop.
codebuddy week12
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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