Skip to content

Commit 322bfeb

Browse files
committed
I can now get into google
1 parent 637065c commit 322bfeb

File tree

5 files changed

+152
-15
lines changed

5 files changed

+152
-15
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,11 @@ Success is like pregnancy, Everybody congratulates you but nobody knows how many
8585
|235|[Lowest Common Ancestor of a Binary Search Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/#/description)| [Python](./tree/Yu/235_lca_bst.py) | _O(N)_| _O(1)_ | Easy | ||
8686

8787

88-
## 5/2 Tasks (Tree Easy/Medium)
88+
## Tree Easy
8989
| # | Title | Solution | Time | Space | Difficulty |Tag| Note|
9090
|-----|-------| -------- | ---- | ------|------------|---|-----|
9191
|108|[Convert Sorted Array to Binary Search Tree](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/#/solutions)| [Python [Yu]](./tree/Yu/108.py) | _O(N)_| _O(N)_ | Easy | |[公瑾讲解](https://www.youtube.com/watch?v=lBrb4fXPcMM)|
92+
|226|[Invert Binary Tree](https://leetcode.com/problems/invert-binary-tree/#/description)| [Python [Yu]](./tree/Yu/226.py) | _O(N)_| _O(1)_ | Easy | |[公瑾讲解](https://youtu.be/oiX3mqcAK0s)|
9293
|563|[Binary Tree Tilt](https://leetcode.com/problems/binary-tree-tilt/#/description)| [Python [Yu]](./tree/Yu/563.py) | _O(N)_| _O(1)_ | Easy | |[公瑾讲解](https://youtu.be/47FQVP4ynk0)|
9394
|257|[Binary Tree Paths](https://leetcode.com/problems/binary-tree-paths/#/description)| [Python [Yu]](./tree/Yu/257.py) | _O(N)_| _O(N)_ | Easy | |[公瑾讲解](https://youtu.be/Zr_7qq2f16k)|
9495
|102|[Binary Tree Level Order Traversal](https://leetcode.com/problems/binary-tree-level-order-traversal/#/description)| [Python [Yu]](./tree/Yu/102.py) | _O(N)_| _O(N)_ | Medium | |[公瑾讲解](https://youtu.be/IWiprpdSgzg)|

stack_queue/template.py

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,40 @@
22
# -*- coding: utf-8 -*-
33

44
# Author: Yu Zhou
5+
# 226. Invert Binary Tree
56

67
# ****************
78
# Descrption:
8-
# Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
9-
#
10-
# push(x) -- Push element x onto stack.
11-
# pop() -- Removes the element on top of the stack.
12-
# top() -- Get the top element.
13-
# getMin() -- Retrieve the minimum element in the stack.
9+
# Invert a binary tree
1410
# ****************
1511

1612
# 思路:
17-
# 创建一个额外的stack用来储存所有的最小值
18-
# 返回Min的时候,就返回minstack的最后一个值就好了
19-
13+
# 每层需要进行左右的互换
14+
# 完了以后向上返回就行
2015

2116

2217
# ****************
2318
# Final Solution *
2419
# ****************
25-
26-
27-
# ***************************************
28-
# The following code is an fail attempt *
29-
# ***************************************
20+
class Solution(object):
21+
def invertTree(self, root):
22+
"""
23+
:type root: TreeNode
24+
:rtype: TreeNode
25+
"""
26+
27+
#Edge
28+
if not root:
29+
return
30+
31+
#Swap
32+
temp = root.left
33+
root.left = root.right
34+
root.right = temp
35+
36+
#Recusion
37+
self.invertTree(root.left)
38+
self.invertTree(root.right)
39+
40+
#Return
41+
return root

tree/Yu/226.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
4+
# Author: Yu Zhou
5+
# 226. Invert Binary Tree
6+
7+
# ****************
8+
# Descrption:
9+
# Invert a binary tree
10+
# ****************
11+
12+
# 思路:
13+
# 每层需要进行左右的互换
14+
# 完了以后向上返回就行
15+
16+
17+
# ****************
18+
# Final Solution *
19+
# ****************
20+
class Solution(object):
21+
def invertTree(self, root):
22+
"""
23+
:type root: TreeNode
24+
:rtype: TreeNode
25+
"""
26+
27+
#Edge
28+
if not root:
29+
return
30+
31+
#Swap
32+
temp = root.left
33+
root.left = root.right
34+
root.right = temp
35+
36+
#Recusion
37+
self.invertTree(root.left)
38+
self.invertTree(root.right)
39+
40+
#Return
41+
return root

tree/Yu/try.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def traversal(root):
2+
if not root:
3+
return None
4+
print root.val
5+
traversal(root.left)
6+
traversal(root.right)

tree/Yu/try2.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# '''
2+
# 1
3+
# / \
4+
# 2 3
5+
# / \ / \
6+
# 1 3 6 2
7+
# \
8+
# 5
9+
#
10+
# 1
11+
# / \
12+
# 3 2
13+
# / \ / \
14+
# 2 6 3 1
15+
# /
16+
# 5
17+
18+
def inverseTree(root):
19+
# if root.left or root.right:
20+
temp = root.left
21+
root.left = root.right
22+
root.right = temp
23+
24+
25+
#
26+
# return 3
27+
#
28+
#
29+
# 2
30+
# \
31+
# 3
32+
# /
33+
# 2
34+
# /
35+
# 1
36+
#
37+
# return 2
38+
#
39+
# class TreeNode(object):
40+
# def __init__(self, x):
41+
# self.val = x
42+
# self.left = None
43+
# self.right = None
44+
#
45+
# 2
46+
# / \
47+
# 0 5
48+
#
49+
# '''
50+
#
51+
#
52+
#
53+
#
54+
# def longestCon(root):
55+
# '''
56+
# input: TreeNode
57+
# output: int
58+
# '''
59+
# def dfs_level(node, max_num):
60+
# if not root:
61+
# return 0
62+
# cur = 1
63+
# left = dfs_level(node.left)
64+
# right = dfs_level(node.right)
65+
#
66+
# if node.left:
67+
# if node.val + 1 == node.left.val:
68+
# cur = max(cur, left + 1)
69+
# if node.right:
70+
# if node.val + 1 == node.right.val:
71+
# cur = max(cur, right + 1)
72+
# max_num = max(max_num, cur)
73+
# return cur
74+
#
75+
# max_num = 0
76+
# dfs_level(root, max_num)
77+
# return max_num

0 commit comments

Comments
 (0)