Skip to content

Commit 4949001

Browse files
committed
Solution of BST LCA and Invert Binary Tree
1 parent 00d87af commit 4949001

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed

200-300q/226.py

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
'''
2+
Invert a binary tree.
3+
4+
Example:
5+
6+
Input:
7+
8+
4
9+
/ \
10+
2 7
11+
/ \ / \
12+
1 3 6 9
13+
14+
Output:
15+
16+
4
17+
/ \
18+
7 2
19+
/ \ / \
20+
9 6 3 1
21+
'''
22+
23+
# Definition for a binary tree node.
24+
# class TreeNode(object):
25+
# def __init__(self, x):
26+
# self.val = x
27+
# self.left = None
28+
# self.right = None
29+
30+
class Solution(object):
31+
def invertTree(self, root):
32+
"""
33+
:type root: TreeNode
34+
:rtype: TreeNode
35+
"""
36+
37+
if not root:
38+
return
39+
40+
leftTree = self.invertTree(root.left)
41+
rightTree = self.invertTree(root.right)
42+
root.left = rightTree
43+
root.right = leftTree
44+
return root
45+
46+
47+
# Definition for a binary tree node.
48+
# class TreeNode(object):
49+
# def __init__(self, x):
50+
# self.val = x
51+
# self.left = None
52+
# self.right = None
53+
54+
class Solution(object):
55+
def invertTree(self, root):
56+
"""
57+
:type root: TreeNode
58+
:rtype: TreeNode
59+
"""
60+
if not root:
61+
return None
62+
63+
queue = [root]
64+
while queue:
65+
node = queue.pop(0)
66+
node.left, node.right = node.right, node.left
67+
if node.left:
68+
queue.append(node.left)
69+
if node.right:
70+
queue.append(node.right)
71+
72+
return root

200-300q/235.py

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'''
2+
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.
3+
4+
According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”
5+
6+
Given binary search tree: root = [6,2,8,0,4,7,9,null,null,3,5]
7+
8+
_______6______
9+
/ \
10+
___2__ ___8__
11+
/ \ / \
12+
0 _4 7 9
13+
/ \
14+
3 5
15+
16+
Example 1:
17+
18+
Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8
19+
Output: 6
20+
Explanation: The LCA of nodes 2 and 8 is 6.
21+
22+
Example 2:
23+
24+
Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4
25+
Output: 2
26+
Explanation: The LCA of nodes 2 and 4 is 2, since a node can be a descendant of itself
27+
according to the LCA definition
28+
'''
29+
30+
# Definition for a binary tree node.
31+
# class TreeNode(object):
32+
# def __init__(self, x):
33+
# self.val = x
34+
# self.left = None
35+
# self.right = None
36+
37+
class Solution(object):
38+
def lowestCommonAncestor(self, root, p, q):
39+
"""
40+
:type root: TreeNode
41+
:type p: TreeNode
42+
:type q: TreeNode
43+
:rtype: TreeNode
44+
"""
45+
if not root:
46+
return None
47+
48+
if root.val > p.val and root.val > q.val:
49+
return self.lowestCommonAncestor(root.left, p, q)
50+
elif root.val < p.val and root.val < q.val:
51+
return self.lowestCommonAncestor(root.right, p, q)
52+
else:
53+
return root

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ Python solution of problems from [LeetCode](https://leetcode.com/).
3737
|239|[Sliding Window Maximum](https://leetcode.com/problems/sliding-window-maximum/)| [Python](./200-300q/239.py)|Hard|
3838
|238|[Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/)| [Python](./200-300q/238.py)|Medium|
3939
|236|[Lowest Common Ancestor of a Binary Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/)| [Python](./200-300q/236.py)|Medium|
40+
|235|[Lowest Common Ancestor of a Binary Search Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/)|[Python](./200-300q/235.py)|Easy|
4041
|234|[Palindrome Linked List](https://leetcode.com/problems/palindrome-linked-list/)| [Python](./200-300q/234.py)|Easy|
4142
|230|[Kth Smallest Element in a BST](https://leetcode.com/problems/kth-smallest-element-in-a-bst/)| [Python](./200-300q/230.py)|Medium|
43+
|226|[Invert Binary Tree](https://leetcode.com/problems/invert-binary-tree)|[Python](./200-300q/226.py)|Easy|
4244
|215|[Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/)| [Python](./200-300q/215.py)|Medium|
4345
|210|[Course Schedule II](https://leetcode.com/problems/course-schedule-ii/)| [Python](./200-300q/210.py)|Medium|
4446
|208|[Implement Trie (Prefix Tree)](https://leetcode.com/problems/implement-trie-prefix-tree/)| [Python](./200-300q/208.py)|Medium|

0 commit comments

Comments
 (0)