100. Same Tree Total Accepted: 157606 Total Submissions: 353631 Difficulty: Easy Given two binary trees, write a function to check if they are equal or not. Two binary trees are considered equal if they are structurally identical and the nodes have the same value. Code # Definition for a binary tree node. # class …
Monthly Archives: October 2016
Leetcode 310: Minimum Height Trees
310. Minimum Height Trees Total Accepted: 20625 Total Submissions: 74043 Difficulty: Medium For a undirected graph with tree characteristics, we can choose any node as the root. The result graph is then a rooted tree. Among all possible rooted trees, those with minimum height are called minimum height trees (MHTs). Given such a graph, …
Leetcode 78: Subsets
78. Subsets Total Accepted: 118197 Total Submissions: 341929 Difficulty: Medium Given a set of distinct integers, nums, return all possible subsets. Note: The solution set must not contain duplicate subsets. For example,If nums = [1,2,3], a solution is: [ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], [] ] Subscribe to see which companies asked …
Leetcode 94: Binary Tree Inorder Traversal
94. Binary Tree Inorder Traversal Total Accepted: 151532 Total Submissions: 358835 Difficulty: Medium Given a binary tree, return the inorder traversal of its nodes’ values. For example:Given binary tree [1,null,2,3], 1 \ 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? Code (recursive) # Definition for a binary …
Continue reading “Leetcode 94: Binary Tree Inorder Traversal”
Leetcode 344: Reverse String
344. Reverse String Total Accepted: 92658 Total Submissions: 159447 Difficulty: Easy Write a function that takes a string as input and returns the string reversed. Example:Given s = “hello”, return “olleh”. Code class Solution(object): def reverseString(self, s): “”” :type s: str :rtype: str “”” return s[::-1] Idea Nothing too much…
Leetcode 151: Reverse Words in a String
151. Reverse Words in a String Total Accepted: 121339 Total Submissions: 769606 Difficulty: Medium Given an input string, reverse the string word by word. For example,Given s = “the sky is blue“,return “blue is sky the“. Update (2015-02-12):For C programmers: Try to solve it in-place in O(1) space. click to show clarification. Clarification: What constitutes …
Leetcode 190: Reverse Bits
Code (bitwise operation) class Solution: # @param n, an integer # @return an integer def reverseBits(self, n): res = 0 for i in xrange(32): res = (res << 1) + (n & 1) n = n >> 1 return res Idea Use n & 1 to get the last bit. Use n >> 1 to shift …
Leetcode 92: Reverse Linked List II
https://leetcode.com/problems/reverse-linked-list-ii/ 92. Reverse Linked List II Total Accepted: 85657 Total Submissions: 293208 Difficulty: Medium Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note:Given m, n satisfy the following condition:1 ≤ m ≤ n ≤ length …
Leetcode 206: Reversed Linked List
https://leetcode.com/problems/reverse-linked-list/ 206. Reverse Linked List Total Accepted: 149090 Total Submissions: 353930 Difficulty: Easy Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either iteratively or recursively. Could you implement both? Code (Iterative) # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # …
Leetcode 130: Surrounding regions
https://leetcode.com/problems/surrounded-regions/ 130. Surrounded Regions Total Accepted: 64022 Total Submissions: 378153 Difficulty: Medium Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A region is captured by flipping all 'O's into 'X's in that surrounded region. For example, X X X X X O O X X …