本文整理汇总了Python中TreeNode.TreeNode.right方法的典型用法代码示例。如果您正苦于以下问题:Python TreeNode.right方法的具体用法?Python TreeNode.right怎么用?Python TreeNode.right使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TreeNode.TreeNode
的用法示例。
在下文中一共展示了TreeNode.right方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test001
# 需要导入模块: from TreeNode import TreeNode [as 别名]
# 或者: from TreeNode.TreeNode import right [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
示例2: test003
# 需要导入模块: from TreeNode import TreeNode [as 别名]
# 或者: from TreeNode.TreeNode import right [as 别名]
def test003(self):
s = Solution()
root = TreeNode('root')
nodeR = TreeNode('nodeR')
nodeRR = TreeNode('nodeRR')
root.right = nodeR
nodeR.right = nodeRR
n = s.maxDepth(root)
print "input:\t", root
print "expect:\t", 3
print "output:\t", n
示例3: create_minimal_binary_tree
# 需要导入模块: from TreeNode import TreeNode [as 别名]
# 或者: from TreeNode.TreeNode import right [as 别名]
def create_minimal_binary_tree(list_sort):
if not list_sort:
return None
if len(list_sort) == 1:
return TreeNode(list_sort[0])
elif len(list_sort) == 2:
root = TreeNode(list_sort[0])
root.right = TreeNode(list_sort[1])
return root
else:
half = len(list_sort) / 2
root = TreeNode(list_sort[half])
root.left = create_minimal_binary_tree(list_sort[0:half])
root.right = create_minimal_binary_tree(list_sort[half+1:])
return root
示例4: linkNode
# 需要导入模块: from TreeNode import TreeNode [as 别名]
# 或者: from TreeNode.TreeNode import right [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]
示例5: build
# 需要导入模块: from TreeNode import TreeNode [as 别名]
# 或者: from TreeNode.TreeNode import right [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
示例6: buildTrees
# 需要导入模块: from TreeNode import TreeNode [as 别名]
# 或者: from TreeNode.TreeNode import right [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
示例7: generateNumTrees
# 需要导入模块: from TreeNode import TreeNode [as 别名]
# 或者: from TreeNode.TreeNode import right [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
示例8: buildTree
# 需要导入模块: from TreeNode import TreeNode [as 别名]
# 或者: from TreeNode.TreeNode import right [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
示例9: construct
# 需要导入模块: from TreeNode import TreeNode [as 别名]
# 或者: from TreeNode.TreeNode import right [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
示例10: gen
# 需要导入模块: from TreeNode import TreeNode [as 别名]
# 或者: from TreeNode.TreeNode import right [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
示例11: _build_tree
# 需要导入模块: from TreeNode import TreeNode [as 别名]
# 或者: from TreeNode.TreeNode import right [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
示例12: sortedArrayToBST
# 需要导入模块: from TreeNode import TreeNode [as 别名]
# 或者: from TreeNode.TreeNode import right [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
示例13: sortedArrayToBSTRecur
# 需要导入模块: from TreeNode import TreeNode [as 别名]
# 或者: from TreeNode.TreeNode import right [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
示例14: sortedArrayToBST
# 需要导入模块: from TreeNode import TreeNode [as 别名]
# 或者: from TreeNode.TreeNode import right [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
示例15: main
# 需要导入模块: from TreeNode import TreeNode [as 别名]
# 或者: from TreeNode.TreeNode import right [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