##Binary Tree Level Order Traversal

leetcode: https://leetcode.com/problems/binary-tree-level-order-traversal/

Given a binary tree, return the level order traversal of its nodes’ values. (ie, from left to right, level by level).

class Solution:

# @param root, a tree node
# @return a list of lists of integers
def levelOrder(self, root):
    #list to save integers
    if not root:
        return []
    result = []
    # a recu 
    self.helper(root, 0, result)
    return result

def helper(self, root, level, result):
    #
    if not root:
        return
    if level+1 > len(result):
            result.append([])
    result[level].append(root.val)
    self.helper(root.left, level+1, result)
    self.helper(root.right, level+1, result)

##Path Sum
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.

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.right is None and root.left is None:
        return root.val == sum
    return self.hasPathSum(root.left, sum - root.val) or self.hasPathSum(root.right, sum - root.val)

##Minimum Depth of Binary Tree

class Solution:

# @param root, a tree node
# @return an integer
def minDepth(self, root):
    if not root:
        return 0
    if not root.left and root.right:
        return self.minDepth(root.right) +1
    if root.left  and not root.right :
        return self.minDepth(root.left) +1
    return min(self.minDepth(root.left), self.minDepth(root.right)) +1    

##Maximum Depth of Binary Tree

class Solution:

# @param root, a tree node
# @return an integer
def maxDepth(self, root):
    if not root:
        return 0
    else:
        return max(self.maxDepth(root.left), self.maxDepth(root.right))+1

##Balanced Binary Tree
Given a binary tree, determine if it is height-balanced.

For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.

class Solution:

# @param root, a tree node
# @return a boolean

        # every node max height
def height(self, root):
    if not root:
        return 0
    return max(self.height(root.left), self.height(root.right)) + 1

def isBalanced(self, root):
    # root is empty, true
    if not root:
        return True
    # every two children node height less than 1 
    if abs(self.height(root.left) - self.height(root.right)) <= 1:
        return self.isBalanced(root.left) and self.isBalanced(root.right)
    # not balance return False
    else:
        return False

#Same Tree

Given two binary trees, write a function to check if they are equal or not.

Two binary trees are considered equal if they are structurally identical and the nodes have the same value.

class Solution:

# @param p, a tree node
# @param q, a tree node
# @return a boolean
def isSameTree(self, p, q):
    if p == q == None:
        return True
    if p and q and q.val == p.val:
        return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)
    return False    

####Symmetric Tree

原题地址:https://oj.leetcode.com/problems/symmetric-tree/

题意:判断二叉树是否为对称的。

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).

解题思路:这题也不难。需要用一个help函数,当然也是递归的。当存在左右子树时,判断左右子树的根节点值是否相等,如果想等继续递归判断左子树根的右子树根节点和右子树根的左子树根节点以及左子树根的左子树根节点和右子树根的右子树根节点的值是否相等。然后一直递归判断下去就可以了。

class Solution:

# @param root, a tree node
# @return a boolean
def isSymmetric(self, root):
    if root:
        return self.helper(root.left, root.right) 
    return True    

def helper(self, p, q):
    if p == q == None:
        return True
    if p and q and  q.val== p.val:
        return self.helper(p.left, q.right) and self.helper(p.right, q.left)
    return False  

#Markdown–从入门到精通

#一级标题

##二级标题

###三级标题

####四级标题

#####五级标题

####无序列

  • 1
  • 2
  • 3

####有序列

  1. 1
  2. 2
  3. 3

####引用

这里是引用

####插入图片

Baidu

####插入图片

####粗体

测试斜体

测试粗体

####表格
| Tables | Are | Cool |
| ————- |:————-:| —–:|
| col 3 is | right-aligned | $1600 |
| col 2 is | centered | $12 |
| zebra stripes | are neat | $1 |

####代码

var a = 1;

Inline code and Block code

Inline code are surround by backtick key. To create a block code:

Indent each line by at least 1 tab, or 4 spaces.
var Mou = exactlyTheAppIwant; 

test2

/**
 * nth element in the fibonacci series.
 * @param n >= 0
 * @return the nth element, >= 0.
 */
function fib(n) {
    var a = 1, b = 1;
    var tmp;
    while (--n >= 0) {
        tmp = a;
        a += b;
       b = tmp;
    }
    return a;
}

document.write(fib(10));  

####分割线