Skip to content

Commit f31d86a

Browse files
Chris WuChris Wu
Chris Wu
authored and
Chris Wu
committed
Update Readme.
binary-tree-maximum-path-sum.py
1 parent e28c38a commit f31d86a

File tree

2 files changed

+85
-15
lines changed

2 files changed

+85
-15
lines changed

README.md

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,32 @@ https://github.com/wuduhren/leetcode-python
88

99
# Resources
1010
These are the interview resources I personally used and only if it is really helpful I will put it here.
11-
## System Design
12-
More resource
13-
<https://github.com/shashank88/system_design>
14-
Architecture 101
15-
<https://engineering.videoblocks.com/web-architecture-101-a3224e126947>
16-
How to scale up? There are also lots of tech interview related topic in his channel.
17-
<https://www.youtube.com/watch?v=yPF94QiI2qk&t=385s>
1811

19-
## Data Structure and Algorithm Basics
20-
Basic data structure and algorithm online course taught in Python. This course is design to help you find job and do well in the interview.
21-
<https://classroom.udacity.com/courses/ud513>
12+
## Overall Mindset
13+
Haveing a right mindset is the most important resource. It keeps you going when you are tired after work. Studying when everyone else are out having fun. Reminding you that your goals are not going to come easy, it takes time, self-discipline, mental and physical toughness...
14+
15+
This is a speech from Sean Lee on "How to Get a Job at the Big 4". I highly recommend this because it gives me the right mindset on how to put in the work.
16+
<https://youtu.be/YJZCUhxNCv8>
17+
18+
CS DoJo on "How I Got a Job at Google as a Software Engineer". There are also lots of technique on coding interview in his channel.
19+
<https://www.youtube.com/watch?v=UPO-9iMjBpc>
2220

2321
## Prepare in a Structural Way
2422
1. <https://www.quora.com/How-should-I-prepare-for-my-Google-interview-if-I-have-1-month-left-and-I%E2%80%99m-applying-for-a-software-engineer-role/answer/Anthony-D-Mays?ch=10&share=5c488000&srid=W0jqp>
2523
2. <https://www.quora.com/How-can-I-get-a-job-at-Facebook-or-Google-in-6-months-I-need-a-concise-work-plan-to-build-a-good-enough-skill-set-Should-I-join-some-other-start-up-or-build-my-own-projects-start-up-Should-I-just-focus-on-practicing-data-structures-and-algorithms/answer/Jimmy-Saade>
2624
3. <https://www.quora.com/What-should-I-know-from-the-CLRS-3rd-edition-book-if-my-aim-is-to-get-into-Google/answer/Jimmy-Saade>
2725

28-
## Overall Mindset
29-
This is a speech from Sean Lee on "How to Get a Job at the Big 4". I highly recommend this because it gives me the right mindset on how to put in the work.
30-
<https://youtu.be/YJZCUhxNCv8>
26+
## Data Structure and Algorithm Basics
27+
Basic data structure and algorithm online course taught in Python. This course is design to help you find job and do well in the interview.
28+
<https://classroom.udacity.com/courses/ud513>
3129

32-
CS DoJo on "How I Got a Job at Google as a Software Engineer". There are also lots of technique on coding interview in his channel.
33-
<https://www.youtube.com/watch?v=UPO-9iMjBpc>
30+
## System Design
31+
More resource
32+
<https://github.com/shashank88/system_design>
33+
Architecture 101
34+
<https://engineering.videoblocks.com/web-architecture-101-a3224e126947>
35+
How to scale up? There are also lots of tech interview related topic in his channel.
36+
<https://www.youtube.com/watch?v=yPF94QiI2qk&t=385s>
3437

3538
## Interview Question Survey
3639
<https://www.glassdoor.com/index.htm>

binary-tree-maximum-path-sum.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
"""
2+
For every subtree, we generate two output.
3+
4+
* The output from the path which can be used by its parent.
5+
* Only the node itself
6+
* The node with its avaliable left branch
7+
* The node with its avaliable right branch
8+
9+
* The output from the path which cannot (or won't) be used by its parent.
10+
* Only the path from the left branch
11+
* Only the path from the right branch
12+
* The node with avaliable left branch and avaliable right branch at the same time
13+
* The output of the first case. The means that even the path can be selected but we do not select it. Because selecting it will generate smaller output.
14+
15+
We initial all the value to be negative infinity, so the it will just be ignored by the `max()` if left or right node is `None`.
16+
`a` means avaliable.
17+
`na` means not avaliable.
18+
"""
19+
#recursive
20+
class Solution(object):
21+
def maxPathSum(self, root):
22+
def helper(node):
23+
left_a = left_na = right_a = right_na = float('-inf')
24+
25+
if node.left:
26+
left_a, left_na = helper(node.left)
27+
if node.right:
28+
right_a, right_na = helper(node.right)
29+
30+
a = max(node.val, node.val+left_a, node.val+right_a)
31+
na = max(left_na, right_na, node.val+left_a+right_a, a)
32+
return a, na
33+
34+
return max(helper(root))
35+
36+
#iterative
37+
class Solution(object):
38+
def maxPathSum(self, root):
39+
pre_stack = [root]
40+
stack = []
41+
memo = {}
42+
43+
while pre_stack:
44+
node = pre_stack.pop()
45+
stack.append(node)
46+
if node.left:
47+
pre_stack.append(node.left)
48+
if node.right:
49+
pre_stack.append(node.right)
50+
51+
while stack:
52+
node = stack.pop()
53+
54+
left_a = left_na = right_a = right_na = float('-inf')
55+
56+
if node.left:
57+
left_a, left_na = memo[node.left]
58+
if node.right:
59+
right_a, right_na = memo[node.right]
60+
61+
a = max(node.val, node.val+left_a, node.val+right_a)
62+
na = max(left_na, right_na, node.val+left_a+right_a, a)
63+
64+
memo[node] = (a, na)
65+
66+
return max(memo[root])
67+

0 commit comments

Comments
 (0)