yan chuanli 2 years ago
parent
commit
e599210496
6 changed files with 52 additions and 140 deletions
  1. 1 1
      .idea/leetcode.iml
  2. 1 1
      .idea/misc.xml
  3. 25 0
      array/yang_hui_triangle.py
  4. BIN
      tree/__pycache__/tree.cpython-38.pyc
  5. 20 0
      tree/sortedArray_to_bst.py
  6. 5 138
      tree/tree.py

+ 1 - 1
.idea/leetcode.iml

@@ -2,7 +2,7 @@
 <module type="PYTHON_MODULE" version="4">
   <component name="NewModuleRootManager">
     <content url="file://$MODULE_DIR$" />
-    <orderEntry type="jdk" jdkName="Python 3.8 (py38) (2)" jdkType="Python SDK" />
+    <orderEntry type="jdk" jdkName="Python 3.8 (py38_schoolcert) (2)" jdkType="Python SDK" />
     <orderEntry type="sourceFolder" forTests="false" />
   </component>
 </module>

+ 1 - 1
.idea/misc.xml

@@ -1,4 +1,4 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <project version="4">
-  <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.8 (py38) (2)" project-jdk-type="Python SDK" />
+  <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.8 (py38_schoolcert) (2)" project-jdk-type="Python SDK" />
 </project>

+ 25 - 0
array/yang_hui_triangle.py

@@ -0,0 +1,25 @@
+from typing import List
+
+
+def generate(numRows: int) -> List[List[int]]:
+    i = 1
+    res = []
+
+    while i <= numRows:
+        if i == 1:
+            res.append([1])
+            i += 1
+
+        elif i == 2:
+            res.append([1, 1])
+            i += 1
+
+        elif i > 2:
+            a = -1
+            res.append([1, i + a, 1])
+            a = i + a
+            i += 1
+
+    return res
+
+print(generate(4))

BIN
tree/__pycache__/tree.cpython-38.pyc


+ 20 - 0
tree/sortedArray_to_bst.py

@@ -0,0 +1,20 @@
+from typing import List, Optional
+from tree import TreeNode
+
+
+class Solution:
+    def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]:
+        if not nums:
+            return
+        mid = len(nums) // 2
+        root = TreeNode(nums[mid])
+
+        if mid == 0:
+            return root
+
+        root.left = self.sortedArrayToBST(nums[:mid])
+        root.right = self.sortedArrayToBST(nums[mid + 1:])
+
+        return root
+
+

+ 5 - 138
tree/tree.py

@@ -1,142 +1,9 @@
-#列表函数
-def BinaryTree(r):
-    return [r, [], []]
 
-#插入左子树
-def insertLeft(root, newBranch):
-    t = root.pop(1)
-    if len(t) > 1:
-        root.insert(1, [newBranch, t, []])
-    else:
-        root.insert(1, [newBranch, [], []])
-    return root
-
-#插入右子树
-def insertRight(root, newBranch):
-    t = root.pop(2)
-    if len(t) > 1:
-        root.insert(2, [newBranch, [], t])
-    else:
-        root.insert(2, [newBranch, [], []])
-    return root
-
-#树的访问函数
-def getRootVal(root):
-    return root[0]
-
-def setRootVal(root, newval):
-    root[0] = newval
-
-def getLeftChild(root):
-    return root[1]
-
-def getRightChild(root):
-    return root[2]
-
-
-# 二叉树的访问函数
-def getLeftChild(self):
-    return self.leftChild
-
-def getRightChild(self):
-    return self.rightChild
-
-def setRootValue(self, obj):
-    self.key = obj
-
-def getRootValue(self):
-    return self.key
-
-
-# 定义节点
-class BinaryTree:
-    def __init__(self, rootObj):
-        self.key = rootObj
-        self.leftChild = None    # 左子节点也是树
-        self.rightChild = None    # 右子节点也是树
-
-# 插入左子节点
-def insertLeft(self, newNode):
-    if self.leftChild == None:
-        self.leftChild = newNode
-    else:
-        t = BinaryTree(newNode)
-        t.leftChild = self.leftChild
-        self.leftChild = t
-
-# 插入右子节点
-def insertRight(self, newNode):
-    if self.rightChild == None:
-        self.rightChild = newNode
-    else:
-        t = BinaryTree(newNode)
-        t.rightChild = self.rightChild
-        self.rightChild = t
-
-# 树的遍历
-
-# 前序
-def preorder(tree):
-    if tree:
-        print(tree.getRootVal())
-        preorder(tree.getLeftChild())
-        preorder(tree.getRightChild())
-
-# 中序
-def inorder(tree):
-    if tree:
-        inorder(tree.getLeftChild())
-        print(tree.getRootVal())
-        inorder(tree.getRightChild())
-
-# 后序
-def postorder(tree):
-    if tree:
-        postorder(tree.getLeftChild())
-        postorder(tree.getRightChild())
-        print(tree.getRootVal())
-
-# 树
-# class Bnode(object):
-#     def __init__(self, data, left=None, right=None):
-#         self.data = data
-#         self.left = left
-#         self.right = right
-#
-# class Btree(object):
-#     def __init__(self, root=None):
-#         self.root = root
-#
-#     @classmethod
-#     def bulid(cls, list):
-#         node_dict = {}
-#         # 制作节点
-#         for item in list:
-#             data = item['data']
-#             node_dict[data] = Bnode(data)
-#         # 把节点组装成数
-#         for item in list:
-#             data = item['data']
-#             node = node_dict[data]
-#             if node['is_root']:
-#                 root = node
-#             node.left = node_dict.get(item['left'])
-#             node.right = node_dict.get(item['right'])
-#         return cls(root)
-#
-#     def preorder(self, subtree):
-#         # 先序
-#         if subtree is not None:
-#             print(subtree.data)
-#             self.preorder(subtree.left)
-#             self.preorder(subtree.right)
-#
-#     def reverse(self, subtree):
-#         # 后序
-#         if subtree is not None:
-#             subtree.left, subtree.right = subtree.right, subtree.left
-#             self.reverse(self, subtree.left)
-#             self.reverse(self, subtree.right)
+class TreeNode:
+    def __init__(self, val=0, left=None, right=None):
+        self.val = val
+        self.left = left
+        self.right = right
 
 # 回溯通用模板
 # res = []