102. Binary Tree Level Order Traversal
BFS iterative:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
Queue<TreeNode> temp =new LinkedList<TreeNode>();
List<List<Integer>> result =new LinkedList <List<Integer>>();
if(root==null) return result;
temp.offer(root);
while(!temp.isEmpty()){
int length=temp.size(); //先求出队列的size,不要放在for循环里面,长度会变!!!
List<Integer> sub =new LinkedList<Integer>();
for(int i=0;i<length;i++){
if(temp.peek().left!=null) temp.offer(temp.peek().left);
if(temp.peek().right!=null) temp.offer(temp.peek().right);
sub.add(temp.poll().val);
}
result.add(sub);
}
return result;
}
}
DFS recursive:O(n)
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> result =new LinkedList<>();
helper(result,root,0);
return result;
}
public void helper(List<List<Integer>> result,TreeNode root,int level){
if(root==null) return;
if(level>=result.size())
result.add(new LinkedList<>());
result.get(level).add(root.val);
helper(result,root.left,level+1);
helper(result,root.right,level+1);
}
}
104. Maximum Depth of Binary Tree
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int maxDepth(TreeNode root) {
if(root==null) return 0;
return 1+ Math.max(maxDepth(root.left),maxDepth(root.right));
}
}
BFS:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int maxDepth(TreeNode root) {
if(root==null) return 0;
int count=0;
Queue<TreeNode> queue =new LinkedList<>();
queue.offer(root);
while(!queue.isEmpty()){
int length=queue.size();
for(int i=0;i<length;i++){
TreeNode node =queue.poll();
if(node.left!=null){
queue.offer(node.left);
}
if(node.right!=null){
queue.offer(node.right);
}
}
count++;
}
return count;
}
}
DFS
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int maxDepth(TreeNode root) {
if(root==null) return 0;
int max=0,temp;
Stack<TreeNode> treenode =new Stack<>();
Stack<Integer> value =new Stack<>();
treenode.push(root);
value.push(1);
while(!treenode.empty()){
TreeNode node =treenode.pop();
temp=value.pop();
max=Math.max(max,temp);
if(node.left!=null){
treenode.push(node.left);
value.push(temp+1);
}
if(node.right!=null){
treenode.push(node.right);
value.push(temp+1);
}
}
return max;
}
}
111. Minimum Depth of Binary Tree
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int minDepth(TreeNode root) {
if(root==null) return 0;
int left=minDepth(root.left);
int right =minDepth(root.right);
return (left==0||right==0)?left+right+1:Math.min(left,right)+1;
}
}
BFS:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
//BFS
class Solution {
public int minDepth(TreeNode root) {
if(root==null )return 0;
Queue<TreeNode> queue =new LinkedList<>();
queue.offer(root);
int depth=1;
while(!queue.isEmpty()){
int length=queue.size();
for(int i=0;i<length;i++){
TreeNode node =queue.poll();
if(node.left==null&&node.right==null){
return depth;
}
if(node.left!=null){
queue.offer(node.left);
}
if(node.right!=null){
queue.offer(node.right);
}
}
depth++;
}
return depth;
}
}
DFS:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int minDepth(TreeNode root) {
if(root==null ) return 0;
if(root.left!=null&&root.right!=null)
return Math.min(minDepth(root.left),minDepth(root.right))+1;
else
return Math.max(minDepth(root.left),minDepth(root.right))+1;
}
}
22. Generate Parentheses
两道数独,两道N皇后
69. Sqrt(x)
二分法:
class Solution {
public int mySqrt(int x) { //二分法
if(x==0||x==1) return x;
int left=0,right=x;
while(true){ //这里要用true,不能用left<=right,否则要在后面有返回值
int mid=left+(right-left)/2;
if(mid>x/mid) right=mid-1;
else{
if((mid+1)>x/(mid+1)) return mid;
left=mid+1;
}
}
}
}
class Solution {
public int mySqrt(int x) { //牛顿法
if(x==0||x==1) return x;
double oldres=x;
double newres=0;
double temp;
while(Math.abs(oldres-newres)>0.01){
temp=(oldres+x/oldres)/2;
newres=oldres;
oldres=temp;
}
return (int) oldres;
}
}
208. Implement Trie (Prefix Tree)
public class TrieNode{
public char val;
public boolean isWord;
public TrieNode[] children =new TrieNode[26];
public TrieNode(){};
TrieNode(char c){
TrieNode node=new TrieNode();
node.val =c;
}
}
class Trie {
private TrieNode root;
/** Initialize your data structure here. */
public Trie() {
root=new TrieNode();
root.val=' ';
}
/** Inserts a word into the trie. */
public void insert(String word) {
TrieNode ws=root;
for(int i=0;i<word.length();i++){
char c=word.charAt(i);
if(ws.children[c-'a']==null){
ws.children[c-'a']=new TrieNode(c);
}
ws=ws.children[c-'a'];
}
ws.isWord=true;
}
/** Returns if the word is in the trie. */
public boolean search(String word) {
TrieNode ws=root;
for(int i=0;i<word.length();i++){
char c=word.charAt(i);
if(ws.children[c-'a']==null) return false;
ws=ws.children[c-'a'];
}
return ws.isWord;
}
/** Returns if there is any word in the trie that starts with the given prefix. */
public boolean startsWith(String prefix) {
TrieNode ws=root;
for(int i=0;i<prefix.length();i++){
char c=prefix.charAt(i);
if(ws.children[c-'a']==null) return false;
ws=ws.children[c-'a'];
}
return true;
}
}
/**
* Your Trie object will be instantiated and called as such:
* Trie obj = new Trie();
* obj.insert(word);
* boolean param_2 = obj.search(word);
* boolean param_3 = obj.startsWith(prefix);
*/
212. Word Search II
191. Number of 1 Bits
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int count=0;
for(int i=0;i<32;i++){
if(n%2!=0) count++;
n= n>>1;
}
return count;
}
}
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int count=0;
while(n!=0){
n=n&(n-1);
count++;
}
return count;
}
}
338. Counting Bits
class Solution {
public int[] countBits(int num) {
int[] count=new int[num+1];
count[0]=0;
for(int i=1;i<=num;i++){
count[i]=count[i&(i-1)]+1;
}
return count;
}
}
class Solution {
public int[] countBits(int num) {
int[] count=new int[num+1];
for(int i=1;i<=num;i++){
int temp=i;
while(temp!=0){
if(temp%2==1) count[i]++;
temp=temp>>1;
}
}
return count;
}
}
231. Power of Two
class Solution {
public boolean isPowerOfTwo(int n) {
if(n<=0) return false;
int count=0;
while(n!=0){
n=n&(n-1);
count++;
}
if(count==1) return true;
return false;
}
}