Skip to content

Commit 26998c1

Browse files
committed
Populating Next Right Pointers in Each Node/ Valid Palindrome
1 parent a1c0877 commit 26998c1

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
'''
2+
Given a binary tree
3+
4+
struct TreeLinkNode {
5+
TreeLinkNode *left;
6+
TreeLinkNode *right;
7+
TreeLinkNode *next;
8+
}
9+
Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.
10+
11+
Initially, all next pointers are set to NULL.
12+
13+
Note:
14+
15+
You may only use constant extra space.
16+
You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children).
17+
For example,
18+
Given the following perfect binary tree,
19+
1
20+
/ \
21+
2 3
22+
/ \ / \
23+
4 5 6 7
24+
After calling your function, the tree should look like:
25+
1 -> NULL
26+
/ \
27+
2 -> 3 -> NULL
28+
/ \ / \
29+
4->5->6->7 -> NULL
30+
'''
31+
32+
# Definition for binary tree with next pointer.
33+
class TreeLinkNode(object):
34+
def __init__(self, x):
35+
self.val = x
36+
self.left = None
37+
self.right = None
38+
self.next = None
39+
40+
41+
class Solution(object):
42+
def connect(self, root):
43+
"""
44+
:type root: TreeLinkNode
45+
:rtype: nothing
46+
"""
47+
if not root:
48+
return
49+
current_level = [root]
50+
while current_level:
51+
next_level = []
52+
for node in current_level:
53+
if node.left:
54+
next_level.append(node.left)
55+
if node.right:
56+
next_level.append(node.right)
57+
for i in range(len(next_level) - 1):
58+
next_level[i].next = next_level[i + 1]
59+
current_level = next_level
60+
61+
62+
if __name__ == "__main__":
63+
None

125 Valid Palindrome.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'''
2+
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
3+
4+
For example,
5+
"A man, a plan, a canal: Panama" is a palindrome.
6+
"race a car" is not a palindrome.
7+
8+
Note:
9+
Have you consider that the string might be empty? This is a good question to ask during an interview.
10+
11+
For the purpose of this problem, we define empty string as valid palindrome.
12+
'''
13+
14+
class Solution(object):
15+
def isPalindrome(self, s):
16+
"""
17+
:type s: str
18+
:rtype: bool
19+
"""
20+
alphanumericS = [c for c in s.lower() if c.isalnum()]
21+
return alphanumericS == alphanumericS[::-1]
22+
23+
24+
if __name__ == "__main__":
25+
assert Solution().isPalindrome("A man, a plan, a canal: Panama") == True
26+
assert Solution().isPalindrome("race a car") == False

0 commit comments

Comments
 (0)