def train(feature): x_train, _, y_train, y_test = train_features[feature] , test_features[feature], \ train_output_labels[feature], test_output_labels[feature] if dev_path: x_dev, y_dev = dev_features[feature], dev_output_labels[feature] x = np.concatenate([x_train, x_dev]) y = np.concatenate([y_train, y_dev]) test_fold = np.concatenate([ np.full(x_train.shape[0], -1, dtype=np.int8), np.zeros(x_dev.shape[0], dtype=np.int8) ]) cv = sklearn.model_selection.PredefinedSplit(test_fold) else: x, y = x_train, y_train cv = None criterion = ['gini', 'entropy'] parameters = { 'criterion': criterion, 'max_depth': np.arange(6, 15), 'min_impurity_decrease': [1e-3] } decision_tree = sklearn.tree.DecisionTreeClassifier() model = GridSearchCV(decision_tree, parameters, cv=cv) model.fit(x, y) trainleave_id = model.best_estimator_.apply(x) uniqueleaves = set(trainleave_id) uniqueleaves = sorted(uniqueleaves) leafcount = {} for i, leaf in enumerate(uniqueleaves): leafcount[i] = round( np.count_nonzero(trainleave_id == leaf) * 100 / len(trainleave_id), 2) feature_names = [] for i in range(len(data_loader.pos_dictionary)): feature_names.append("head@" + data_loader.pos_id2tag[i]) for i in range(len(data_loader.pos_dictionary)): feature_names.append("child@" + data_loader.pos_id2tag[i]) for i in range(len(data_loader.relation_dictionary)): feature_names.append("relation@" + data_loader.relation_id2tag[i]) feature_names.append(feature + "@child") feature_names.append(feature + "@head") tree_rules = export_text(model.best_estimator_, feature_names=feature_names, max_depth=model.best_params_["max_depth"]) treelines = utils.printTreeForBinaryFeatures( tree_rules, data_loader.pos_id2tag, data_loader.relation_id2tag, data_loader.used_relations, data_loader.used_head_pos, data_loader.used_child_pos, feature) leaves = utils.constructTree(treelines, feature) assert len(leaves) == len(leafcount) dev_samples = len(x_dev) if dev_path else 0 printTreeWithExamplesPDF(model, tree_rules, treelines, leaves, feature, leafcount, feature_names, len(y_test), len(x_train), dev_samples)
s = [] cur = root while cur != None or len(s) > 0: if cur != None: result.append(cur.val) s.append(cur) cur = cur.left else: # 此时,必然是访问到最左节点后,当前节点为空,只能出栈 cur = s.pop(-1) cur = cur.right return result def preOrder_II(self, root): # 前序 # 思想很简单,前序遍历是一个中左右的顺序,刚好符合栈的一个入栈出栈的顺序 result = [] s = [] s.append(root) while len(s) != 0: node = s.pop() if node == None: continue result.append(node.val) s.append(node.right) s.append(node.left) return result solu = Solution() root = constructTree() print(solu.preOrder_I(root)) print(solu.preOrder_II(root))
from utils import constructTree class Solution: def dfs(self, root, value): if root.left is None and root.right is None: self.result += value * 10 + root.val return if root.left is not None: self.dfs(root.left, value * 10 + root.val) if root.right is not None: self.dfs(root.right, value * 10 + root.val) def sumNumbers(self, root): """ :type root: TreeNode :rtype: int """ self.result = 0 if root is None: return 0 self.dfs(root, 0) return self.result root = constructTree([4, 9, 0, 5, 1]) s = Solution() print(s.sumNumbers(root))
return getMostLeftNumber(node.left, 2 * number - 1) node = root number = 1 result = 1 while node.left is not None and node.right is not None: leftNumber = getMostLeftNumber(node.left, 2 * number) rightNumber = getMostLeftNumber(node.right, 2 * number + 1) if leftNumber < rightNumber: result = max(result, rightNumber) node = node.right number = 2 * number + 1 else: result = max(result, leftNumber) node = node.left number = 2 * number if node.left is None and node.right is None: return number if node.left is not None and node.right is None: return 2 * number return self.result s = Solution() for i in range(100): root = constructTree(list(range(i))) ans = s.countNodes(root) if ans != i: print('Error:', i, ans)
self.maxSingleValue = max(self.maxSingleValue, node.val) values = [node.val] allValue = node.val if node.left is not None: leftValue = self.getMaxValue(node.left) values.append(leftValue + node.val) allValue += leftValue if node.right is not None: rightValue = self.getMaxValue(node.right) values.append(rightValue + node.val) allValue += rightValue result = max(values) if self.result < max(result, allValue): self.result = max(result, allValue) return max(result, 0) def maxPathSum(self, root): """ :type root: TreeNode :rtype: int """ self.result = root.val self.maxSingleValue = root.val self.getMaxValue(root) return self.result if self.maxSingleValue >= 0 else self.maxSingleValue root = constructTree([1, -2, -3, 1, 3, -2, None, -1]) s = Solution() print(s.maxPathSum(root))
self.val = x self.left = None self.right = None class Solution: def binaryTreePaths(self, root): """ :type root: TreeNode :rtype: List[str] """ result = [] if not root: return [] def dfs(node, substring): if not node.left and not node.right: result.append(substring + str(node.val)) else: if node.left: dfs(node.left, substring + str(node.val) + "->") if node.right: dfs(node.right, substring + str(node.val) + "->") dfs(root, "") return result root = constructTree([i for i in range(7)]) s = Solution() print(s.binaryTreePaths(root))
from utils import constructTree # Definition for a binary tree node. class TreeNode: def __init__(self, x): self.val = x self.left = None self.right = None class Solution: def invertTree(self, root): """ :type root: TreeNode :rtype: TreeNode """ if root is None: return root tmp1 = root.left tmp2 = root.right root.left = tmp2 root.right = tmp1 if root.left is not None: self.invertTree(root.left) if root.right is not None: self.invertTree(root.right) return root root = constructTree([4, 2, 7, 1, 3, 6, 9]) s = Solution() print(s.invertTree(root))
self.right = None class Solution: def lowestCommonAncestor(self, root, p, q): """ :type root: TreeNode :type p: TreeNode :type q: TreeNode :rtype: TreeNode """ m = p.val n = q.val if m == n: return p if m > n: m, n = n, m def findCommon(node, p, q): if p == node.val or q == node.val: return node if p < node.val and q > node.val: return node if q < node.val: return findCommon(node.left, p, q) return findCommon(node.right, p, q) return findCommon(root, m, n) # root = constructTree([6,2,8,0,4,7,9,None,None,3,5]) # s = Solution() # print(s.lowestCommonAncestor(root, TreeNode(2), TreeNode(8))) # print(s.lowestCommonAncestor(root, TreeNode(2), TreeNode(4))) root = constructTree([2, 1, 3]) s = Solution() print(s.lowestCommonAncestor(root, TreeNode(3), TreeNode(1)).val) # print(s.lowestCommonAncestor(root, TreeNode(2), TreeNode(4)))
self.val = x self.left = None self.right = None class Solution: def kthSmallest(self, root, k): """ :type root: TreeNode :type k: int :rtype: int """ self.k = k def dfs(node): if node.left is not None: tmp = dfs(node.left) if tmp is not None: return tmp self.k -= 1 if self.k == 0: return node.val if node.right is not None: tmp = dfs(node.right) if tmp is not None: return tmp return dfs(root) root = constructTree([3, 1, 4, None, 2]) s = Solution() print(s.kthSmallest(root, 1))