/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List> levelOrder(TreeNode root) {
Queue temp =new LinkedList();
List> result =new LinkedList >();
if(root==null) return result;
temp.offer(root);
while(!temp.isEmpty()){
int length=temp.size(); //先求出队列的size,不要放在for循环里面,长度会变!!!
List sub =new LinkedList();
for(int i=0;i
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List> levelOrder(TreeNode root) {
List> result =new LinkedList<>();
helper(result,root,0);
return result;
}
public void helper(List> 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);
}
}
/**
* 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 queue =new LinkedList<>();
queue.offer(root);
while(!queue.isEmpty()){
int length=queue.size();
for(int i=0;i
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 =new Stack<>();
Stack 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;
}
}
/**
* 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 queue =new LinkedList<>();
queue.offer(root);
int depth=1;
while(!queue.isEmpty()){
int length=queue.size();
for(int i=0;i
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;
}
}
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;
}
}
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
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;
}
}
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;
}
}
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;
}
}