This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def grayCode(self, n: int) -> List[int]: | |
res = [0] | |
add = 1 | |
for _ in range(n): | |
for i in range(add): | |
res.append(res[add - 1 - i] | add); | |
add <<= 1 | |
return res |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public List<Integer> grayCode(int n) { | |
List<Integer> res = new ArrayList<>(); | |
res.add(0); | |
if (n > 0) { | |
int adder = 1; | |
for (int i = 1; i <= n; i++) { | |
int len = res.size(); | |
for (int j = len - 1; j >= 0; j--) { | |
res.add(res.get(j) + adder); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for singly-linked list. | |
* public class ListNode { | |
* int val; | |
* ListNode next; | |
* ListNode(int x) { val = x; } | |
* } | |
*/ | |
class Solution { | |
public ListNode reverseBetween(ListNode head, int m, int n) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Definition for singly-linked list. | |
# class ListNode: | |
# def __init__(self, x): | |
# self.val = x | |
# self.next = None | |
class Solution: | |
def reverseBetween(self, head: ListNode, m: int, n: int) -> ListNode: | |
if m == n: return head | |
st = [] | |
left, node = None, head |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for a binary tree node. | |
* public class TreeNode { | |
* int val; | |
* TreeNode left; | |
* TreeNode right; | |
* TreeNode(int x) { val = x; } | |
* } | |
*/ | |
class Solution { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Definition for a binary tree node. | |
# class TreeNode: | |
# def __init__(self, x): | |
# self.val = x | |
# self.left = None | |
# self.right = None | |
class Solution: | |
def isSameTree(self, p: TreeNode, q: TreeNode) -> bool: | |
if not p and not q: return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for a binary tree node. | |
* public class TreeNode { | |
* int val; | |
* TreeNode left; | |
* TreeNode right; | |
* TreeNode(int x) { val = x; } | |
* } | |
*/ | |
class Solution { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Definition for a binary tree node. | |
# class TreeNode: | |
# def __init__(self, x): | |
# self.val = x | |
# self.left = None | |
# self.right = None | |
class Solution: | |
def isSymmetric(self, root: TreeNode) -> bool: | |
if not root: return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public boolean isSymmetric(TreeNode root) { | |
Queue<TreeNode> q = new LinkedList<>(); | |
if (root == null) return true; | |
q.offer(root.left); | |
q.offer(root.right); | |
while (!q.isEmpty()) { | |
TreeNode l = q.poll(); | |
TreeNode r = q.poll(); | |
if (l == null && r == null) continue; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import deque | |
class Solution: | |
def isSymmetric(self, root: TreeNode) -> bool: | |
if not root: return True | |
queue = deque([(root.left, root.right)]) | |
while queue: | |
node1, node2 = queue.popleft() | |
if not node1 and not node2: | |
continue | |
if node1 and node2 and node1.val == node2.val: |