本文整理汇总了Python中TreeNode.TreeNode.left方法的典型用法代码示例。如果您正苦于以下问题:Python TreeNode.left方法的具体用法?Python TreeNode.left怎么用?Python TreeNode.left使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TreeNode.TreeNode
的用法示例。
在下文中一共展示了TreeNode.left方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: generateNumTrees
# 需要导入模块: from TreeNode import TreeNode [as 别名]
# 或者: from TreeNode.TreeNode import left [as 别名]
def generateNumTrees(nums):
if len(nums) < 1:
return []
if 1 == len(nums):
return [TreeNode(nums[0])]
res = []
print(nums)
for i, n in enumerate(nums):
lList, rList = nums[:i], nums[i + 1:]
print('\t', lList, rList)
if 0 == len(lList) and 0 == len(rList):
res.append(TreeNode(n))
elif 0 == len(lList):
for rNode in generateNumTrees(rList):
node = TreeNode(n)
node.right = rNode
res.append(node)
elif 0 == len(rList):
for lNode in generateNumTrees(lList):
node = TreeNode(n)
node.left = lNode
res.append(node)
else:
for lNode in generateNumTrees(lList):
for rNode in generateNumTrees(rList):
node = TreeNode(n)
node.left, node.right = lNode, rNode
res.append(node)
return res
示例2: test001
# 需要导入模块: from TreeNode import TreeNode [as 别名]
# 或者: from TreeNode.TreeNode import left [as 别名]
def test001(self):
print "test 001"
# hxl: see http://en.wikipedia.org/wiki/Tree_traversal
nA = TreeNode('A')
nB = TreeNode('B')
nC = TreeNode('C')
nD = TreeNode('D')
nE = TreeNode('E')
nF = TreeNode('F') # hxl: root
nG = TreeNode('G')
nH = TreeNode('H')
nI = TreeNode('I')
nF.left = nB
nF.right = nG
nB.left = nA
nB.right = nD
nG.right = nI
nD.left = nC
nD.right = nE
nI.left = nH
r = Solution().levelOrderBottom(nF)
print " input:\t", nF
print " output:\t", r
print
示例3: _build_tree
# 需要导入模块: from TreeNode import TreeNode [as 别名]
# 或者: from TreeNode.TreeNode import left [as 别名]
def _build_tree(self, X, y):
'''
INPUT:
- X: 2d numpy array
- y: 1d numpy array
OUTPUT:
- TreeNode
Recursively build the decision tree. Return the root node.
'''
node = TreeNode()
index, value, splits = self._choose_split_index(X, y)
if index is None or len(np.unique(y)) == 1:
node.leaf = True
node.classes = Counter(y)
node.name = node.classes.most_common(1)[0][0]
else:
X1, y1, X2, y2 = splits
node.column = index
node.name = self.feature_names[index]
node.value = value
node.categorical = self.categorical[index]
node.left = self._build_tree(X1, y1)
node.right = self._build_tree(X2, y2)
return node
示例4: build
# 需要导入模块: from TreeNode import TreeNode [as 别名]
# 或者: from TreeNode.TreeNode import left [as 别名]
def build(self, inorder, postorder, startIndexInorder, endIndexInorder, startIndexPostorder, endIndexPostorder):
if len(inorder) == 0 or len(inorder) != len(postorder) or startIndexInorder > endIndexInorder or startIndexPostorder > endIndexPostorder:
return None
# key concept to understand is, when reaching root of inorder,
# count of all left children are the same as postorder
rootNum = postorder[endIndexPostorder]
count = 0
while startIndexInorder + count <= endIndexInorder and inorder[startIndexInorder + count] != rootNum:
count += 1
# i is at root number, start copying
root = TreeNode(rootNum)
# @note:@memorize: here cannot use 'i - 1' as end of posterorder
# eg. 2nd recursion, 'i - 1' = 1, that's for inorder
# so it should be the ***count*** of how many nodes
# root.left = self.build(inorder, postorder, startIndexInorder, i - 1, startIndexPostorder, i - 1)
root.left = self.build(inorder, postorder, startIndexInorder, startIndexInorder + count - 1, startIndexPostorder, startIndexPostorder + count - 1)
root.right = self.build(inorder, postorder, startIndexInorder + count + 1, endIndexInorder, startIndexPostorder + count, endIndexPostorder - 1)
return root
开发者ID:keypointt,项目名称:leetcode,代码行数:28,代码来源:Construct_Binary_Tree_from_Inorder_and_Postorder_Traversal.py
示例5: buildTrees
# 需要导入模块: from TreeNode import TreeNode [as 别名]
# 或者: from TreeNode.TreeNode import left [as 别名]
def buildTrees(self, start, end):
if start > end:
# @note:@memorize: should be something, [[]] or [None]
# or the below for loop of left/right subtree will be skipped
# return []
return [None]
if start == end: # reaching single node
return [TreeNode(start)]
result = []
for i in range(start, end + 1): # end should be included
# oneRoot = TreeNode(i) # @note: should be in inner loop
leftSubtreeArray = self.buildTrees(start, i - 1)
rightSubtreeArray = self.buildTrees(i + 1, end)
for oneLeftSubtree in leftSubtreeArray:
for oneRightSubtree in rightSubtreeArray:
# @note:@memorize: should be here, every append is a brand new tree
# or else, here is changing the reference for the same tree
oneRoot = TreeNode(i)
oneRoot.left = oneLeftSubtree
oneRoot.right = oneRightSubtree
result.append(oneRoot)
return result
示例6: gen
# 需要导入模块: from TreeNode import TreeNode [as 别名]
# 或者: from TreeNode.TreeNode import left [as 别名]
def gen(l,r):
if l> r:
return [None]
ans =[]
for i in range(l,r+1):
ltrees = gen(l,i-1)
rtrees = gen(i+1,r)
for j in range(len(ltrees)):
for k in range(len(rtrees)):
root = TreeNode(i)
root.left = ltrees [j]
root.right = rtrees [k]
ans.append(root)
# if ltrees != [None]:
# for lt in ltrees:
# root.left = lt
# if rtrees != [None]:
# for rt in rtrees:
# root.right = rt
# ans.append(root)
# else:
# root.right = None
# ans.append(root)
# else:
# root.left = None
# if rtrees != [None]:
# for rt in rtrees:
# root.right = rt
# ans.append(root)
# else:
# root.right = None
# ans.append(root)
return ans
示例7: test005
# 需要导入模块: from TreeNode import TreeNode [as 别名]
# 或者: from TreeNode.TreeNode import left [as 别名]
def test005(self):
s = Solution()
root = TreeNode('root')
nodeR = TreeNode('nodeR')
nodeL = TreeNode('nodeL')
nodeRR = TreeNode('nodeRR')
nodeRL = TreeNode('nodeRL')
root.right = nodeR
root.left = nodeL
nodeR.right = nodeRR
nodeR.left = nodeRL
n = s.maxDepth(root)
print "input:\t", root
print "expect:\t", 3
print "output:\t", n
示例8: linkNode
# 需要导入模块: from TreeNode import TreeNode [as 别名]
# 或者: from TreeNode.TreeNode import left [as 别名]
def linkNode(self, nodes, idx):
if 0 == len(nodes):
return None
nodes[idx] = TreeNode(nodes[idx])
nodes[idx].left = self.linkNode(nodes[:idx], idx // 2)
nodes[idx].right = self.linkNode(nodes[idx + 1:], (len(nodes) - idx - 1 - 1) // 2)
return nodes[idx]
示例9: buildTree
# 需要导入模块: from TreeNode import TreeNode [as 别名]
# 或者: from TreeNode.TreeNode import left [as 别名]
def buildTree(self, inorder, postorder):
"""
:type inorder: List[int]
:type postorder: List[int]
:rtype: TreeNode
"""
if len(inorder) == 0 or len(inorder) != len(postorder):
return None
print 'inorder: ', inorder
print 'postorder: ', postorder
# key concept to understand is, when reaching root of inorder,
# count of all left children are the same as postorder
rootNum = postorder[-1]
i = 0
while i < len(inorder) and inorder[i] != rootNum:
i += 1
# i is at root number, start copying
root = TreeNode(rootNum)
root.left = self.buildTree(copy.deepcopy(inorder[0: i]), copy.deepcopy(postorder[0: i]))
# @note:@memorize: here is the key, need to skip root node:
# for inorder, it's list[i], so 2 halves: [0:i] and [i+1:]
# for postorder, it's list[-1], so 2 halves: [0:i] and [i: -1]
# root.right = self.buildTree(copy.deepcopy(inorder[i + 1: ]), copy.deepcopy(postorder[i + 1: ]))
root.right = self.buildTree(copy.deepcopy(inorder[i + 1: ]), copy.deepcopy(postorder[i: -1]))
return root
开发者ID:keypointt,项目名称:leetcode,代码行数:36,代码来源:Construct_Binary_Tree_from_Inorder_and_Postorder_Traversal.py
示例10: construct
# 需要导入模块: from TreeNode import TreeNode [as 别名]
# 或者: from TreeNode.TreeNode import left [as 别名]
def construct(l):
if l is None or 0 == len(l):
return None
node = TreeNode(l[0])
node.left = construct([i for i in l if i < node.val])
node.right = construct([i for i in l if node.val < i])
return node
示例11: sortedArrayToBST
# 需要导入模块: from TreeNode import TreeNode [as 别名]
# 或者: from TreeNode.TreeNode import left [as 别名]
def sortedArrayToBST(self, nums):
if not nums:
return None
mid = len(nums) / 2
root = TreeNode(nums[mid])
root.left = self.sortedArrayToBST(nums[:mid])
root.right = self.sortedArrayToBST(nums[mid+1:])
return root
示例12: doit
# 需要导入模块: from TreeNode import TreeNode [as 别名]
# 或者: from TreeNode.TreeNode import left [as 别名]
def doit():
val = next(vals)
if val == "#":
return None
node = TreeNode(int(val))
node.left = doit()
node.right = doit()
return node
示例13: sortedArrayToBST
# 需要导入模块: from TreeNode import TreeNode [as 别名]
# 或者: from TreeNode.TreeNode import left [as 别名]
def sortedArrayToBST(self, array):
length = len(array)
if length == 0: return None
if length == 1: return TreeNode(array[0])
root = TreeNode(array[length / 2])
root.left = self.sortedArrayToBST(array[:length / 2])
root.right = self.sortedArrayToBST(array[length / 2 + 1:])
return root
示例14: sortedArrayToBSTRecur
# 需要导入模块: from TreeNode import TreeNode [as 别名]
# 或者: from TreeNode.TreeNode import left [as 别名]
def sortedArrayToBSTRecur(self, nums):
if nums is None or 0 == len(nums):
return None
mid = len(nums) // 2
node = TreeNode(nums[mid])
node.left = self.sortedArrayToBSTRecur(nums[:mid])
node.right = self.sortedArrayToBSTRecur(nums[mid + 1:])
return node
示例15: main
# 需要导入模块: from TreeNode import TreeNode [as 别名]
# 或者: from TreeNode.TreeNode import left [as 别名]
def main():
s = Solution()
root = TreeNode(1)
root.left = TreeNode(2)
root.right = TreeNode(3)
root.left.right = TreeNode(5)
result = s.binaryTreePaths(root)
print result