去target的时候忍不住买了balance emulsion。 然后回来不知道怎么用。
然后就发现了这个妹子的博文。
留着借鉴:)
A MEMO FROM ME
sum = 22, 5
/ \
4 8
/ / \
11 13 4
/ \ \
7 2 1
5->4->11->2 which sum is 22. 5
/ \ given this tree and sum = 5, it is not a valid path since
4 8 it doesn't get down to the leaf.
# Definition for a binary tree node
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
# @param root, a tree node
# @param sum, an integer
# @return a boolean
def hasPathSum(self, root, sum):
if root is None:
return False
if root.val == sum and root.left is None and root.right is None:
return True
if root.val != sum and root.left is None and root.right is None:
return False
else:
return self.hasPathSum(root.left,sum-root.val) or self.hasPathSum(root.right, sum-root.val)
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
# @param root, a tree node
# @return a boolean
def __init__(self):
self.dp = {}
def height(self, root):
if root is None:
return 0
else:
lh, rh = self.height(root.left), self.height(root.right)
rh = max(lh, rh) + 1
self.dp[root.left], self.dp[root.right] =lh, rh
return h
def isBalanced(self, root):
if root is None:
return True
else:
# this checks 1.whether is current tree is balanced; 2. whether its two sub trees are balanced
return abs(self.height(root.left)-self.height(root.right)) <= 1 and self.isBalanced(root.left) and self.isBalanced(root.right)
def findMid(head):
if head == None or head.next == None:
return head
slow, fast = head, head
while fast != None and fast.next != None:
fast, slow = fast.next, slow.next
if fast.next == None:
break
else:
fast = fast.next
def findMid(head):
if head == None or head.next == None:
return head
dummy = ListNode(0)
dummy.next = head
slow, fast = dummy, dummy
while fast != None and fast.next != None:
fast, slow = fast.next, slow.next
if fast.next == None:
break
else:
fast = fast.next
class ListNode: def __init__(self,val): self.val = val self.next = None def reverseLinkedList(head): if head == None or head.next == None: return head dummy = ListNode(0) dummy.next = head cur = head.next while cur != None: newcur = cur.next tstart = dummy.next dummy.next = cur cur.next = tstart head.next = newcur cur = newcur return dummy.next ## test block a = ListNode(1) b = ListNode(2) c = ListNode(3) d = ListNode(4) a.next = b b.next = c c.next = d x = reverseLinkedList(a) while x is not None: print(x.val) x = x.nextThis is the way that uses dummy head, but actually you dont really need the dummy, in this iterative function, the only nodes that needed to be tracked is the new head and the current node, which is the one to be added to the front, and until current is None, the traversal through the entire list is done, and the you return the current new head, and that's it!
def reverseLinkedList(head):
if head == None or head.next == None:
return head
# last is the new head, and also is the real 'last' node of the current node, it has a meaning :)
last = haed
current = head.next
while current is not None:
head.next = current.next
current.next = last
last = current
current = head.next
return last
The above code is also "in-place" reverse. maybe it would be a bit concise :)
def inorder(root):
if root == None:
return []
else:
lt = inorder(root.left)
rt = inorder(root.right)
return lt + [root.val] + rt
def factorial(n):
if n == 1:
return 1
else:
return n*factorial(n-1)
This definition is not tail-recursive since the recursive call to factorial is not the last thing in the function (its result has to be multiplied by n)Example that is a Tail Recursion:
def factorial(n, accumulator):
if n == 1:
return accumulator
else:
return factorial(n-1, n * accumulator)
def factorial(n, accumulator):
c = n
while c != 1:
accumulator *= c
return accumulator
Recursive procedures call themselves to work towards a solution to a problem. In simple implementations this balloons the stack as the nesting gets deeper and deeper, reaches the solution, then returns through all of the stack frames. This waste is a common complaint about recursive programming in general.Let's look back at recursion of inorder traversal:to be continued
RANGE Num Orders Total Value
0- 99
100- 999
1000-9999
10000-