
二叉树
独鹿
啊哈
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
letcode153Find Minimum in Rotated Sorted Array
class Solution(object): def findMin(self, nums): """ :type nums: List[int] :rtype: int """ l,r=0,len(nums)-1 while l mid=l+(r-l)原创 2017-05-11 12:41:19 · 236 阅读 · 0 评论 -
leetcode 109Convert Sorted List to Binary Search Tree
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None # Definition for a binary tree node. # class Tre原创 2017-05-08 17:19:00 · 244 阅读 · 0 评论 -
[leetcode108]Convert Sorted Array to Binary Search Tree
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution(objec原创 2017-05-09 21:14:45 · 235 阅读 · 0 评论 -
Binary Tree Inorder Traversal
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution(objec原创 2017-06-07 14:33:38 · 188 阅读 · 0 评论 -
594. Longest Harmonious Subsequence
class Solution(object): def findLHS(self, nums): """ :type nums: List[int] :rtype: int """ count = collections.Counter(nums) ans = 0原创 2017-06-08 13:52:34 · 351 阅读 · 0 评论 -
101Symmetric Tree判断对称二叉树dfs
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution(objec原创 2017-07-04 10:34:12 · 360 阅读 · 0 评论 -
110. Balanced Binary Tree验证平衡二叉树
class Solution(object): def isBalanced(self, root): """ :type root: TreeNode :rtype: bool """ if not root: return True left = se原创 2017-07-04 11:26:04 · 321 阅读 · 0 评论 -
257. Binary Tree Paths打印二叉树路径
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution(objec原创 2017-07-04 17:03:34 · 396 阅读 · 0 评论 -
112. Path Sum
class Solution(object): def hasPathSum(self, root, sum): """ :type root: TreeNode :type sum: int :rtype: bool """ if not root: r原创 2017-07-04 19:58:01 · 309 阅读 · 0 评论 -
python定义树
用python自定义树,先定义节点,然后在把节点合成树。class TreeNode(object): def __init__(self,root,left=None,right=None): self.root = root self.left = left self.right = right A = TreeNode('A') B =...原创 2018-03-30 20:11:08 · 3113 阅读 · 0 评论 -
162. Find Peak Element
class Solution(object): def findPeakElement(self, nums): """ :type nums: List[int] :rtype: int """ l=0 r=len(nums)-1 while l原创 2017-05-25 19:29:26 · 256 阅读 · 0 评论 -
leetcode100same tree
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution(objec原创 2017-05-06 13:30:16 · 261 阅读 · 0 评论 -
leetcode154Find Minimum in Rotated Sorted Array II
class Solution(object): def findMin(self, nums): """ :type nums: List[int] :rtype: int """ l,r=0,len(nums)-1 while l=nums[r]: mi原创 2017-05-11 12:53:46 · 259 阅读 · 0 评论 -
leetcode167Two Sum II - Input array is sorted
这个算法易于理解但是效果不是很好 class Solution(object): def twoSum(self, numbers, target): """ :type numbers: List[int] :type target: int :rtype: List[int] """原创 2017-05-11 14:23:35 · 253 阅读 · 0 评论 -
二叉树的遍历
1.二叉树遍历方式:先序遍历,后续遍历,中序遍历,层次遍历 先 if(BT): getRoot left right 中 if (BT) left get root right 后 if(BT): left right getroot 每个节点遇到三次,先序是第一次取数字,中序点二次取,后续点三次取。路径一样,出口一样,入口一样原创 2017-05-24 10:14:12 · 245 阅读 · 0 评论 -
Construct Binary Tree from Preorder and Inorder Traversal
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution(objec原创 2017-05-24 11:13:25 · 262 阅读 · 0 评论 -
106Construct Binary Tree from Inorder and Postorder Traversal
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution(object):原创 2017-05-24 12:30:57 · 234 阅读 · 0 评论 -
leetcode81Search in Rotated Sorted Array II
class Solution(object): def search(self, nums, target): """ :type nums: List[int] :type target: int :rtype: bool """ if not nums:原创 2017-05-05 21:17:22 · 326 阅读 · 0 评论 -
LEETCODE77Combinations
class Solution(object): def combine(self, n, k): """ :type n: int :type k: int :rtype: List[List[int]] candidates=[x for x in range(1,n+1)] i原创 2017-05-05 22:54:23 · 214 阅读 · 0 评论 -
leetcode74Search a 2D Matrix
class Solution(object): def searchMatrix(self, matrix, target): """ :type matrix: List[List[int]] :type target: int :rtype: bool """ if not m原创 2017-05-05 23:17:50 · 304 阅读 · 0 评论 -
LEETCODE73Set Matrix Zeroes
class Solution(object): def setZeroes(self, matrix): """ :type matrix: List[List[int]] :rtype: void Do not return anything, modify matrix in-place instead. """原创 2017-05-05 23:26:46 · 317 阅读 · 0 评论 -
二叉树中和为给定值的所有路径
本质上是压栈出栈的过程1.写一个主题函数主题函数中要给定默认的树,给定的值,还要一个用来存路径的列表,再加一个记录路径长度的变量2.写一个找路径的函数首先要确定给的节点是不是一个空的,如果是空的则直接退出;其次,把当前节点加入到路径,把当前节点值加进路径长度然后,判断当前是不是叶子(叶子用一个变量表示)如果是叶子,则再判断叶子路径和和给定值是不是一样,一样则打印路径,不一样则不管如果不是叶子,则分...原创 2018-03-30 22:42:07 · 376 阅读 · 0 评论