Skip to content

Commit

Permalink
added more
Browse files Browse the repository at this point in the history
  • Loading branch information
QingguoC committed Jan 2, 2018
1 parent 8de33cb commit 7ce7363
Show file tree
Hide file tree
Showing 160 changed files with 31,678 additions and 0 deletions.
290 changes: 290 additions & 0 deletions 100. Same Tree.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,290 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Given two binary trees, write a function to check if they are equal or not.\n",
"\n",
"Two binary trees are considered equal if they are structurally identical and the nodes have the same value."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"class TreeNode(object):\n",
" def __init__(self,x):\n",
" self.val=x\n",
" self.left=None\n",
" self.right=None\n",
" \n",
"\n",
"class Solution(object):\n",
" def isSameTree(self, p, q):\n",
" \"\"\"\n",
" :type p: TreeNode\n",
" :type q: TreeNode\n",
" :rtype: bool\n",
" \"\"\"\n",
" if p is None and q is None:\n",
" return True\n",
" if p is None or q is None:\n",
" return False\n",
" if p.val != q.val:\n",
" return False\n",
" if not self.isSameTree(p.left,q.left):\n",
" return False\n",
" if not self.isSameTree(p.right,q.right):\n",
" return False\n",
" return True\n",
" \n",
" "
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"n1=TreeNode(1)\n",
"\n",
"n2=TreeNode(1)\n",
"n1.left=n3\n",
"n3=TreeNode(1)\n",
"n2.left=n3"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"s=Solution()"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s.isSameTree(n1,n2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"[1, 4, 9, 25]"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x=[1,2,3,5]\n",
"list(map(lambda x:x**2,x))"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"x=['a','b','a']"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"'b'"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"chr(ord('a')^ord('b')^ord(x[0]))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"97"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ord(x[0])"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"['b', 'a', 'b']"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[chr(ord('a')^ord('b')^ord(xi)) for xi in x]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<map object at 0x10494b278>\n"
]
}
],
"source": [
"print(map(lambda t: chr(ord('a')^ord('b')^ord(t)), x))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python [default]",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Loading

0 comments on commit 7ce7363

Please sign in to comment.