LeetCode 102_104_111_22_69_208_191_338_231

102. Binary Tree Level Order Traversal

LeetCode 102_104_111_22_69_208_191_338_231_第1张图片
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> 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

LeetCode 102_104_111_22_69_208_191_338_231_第2张图片
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> 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);
    }
}

LeetCode 102_104_111_22_69_208_191_338_231_第3张图片

104. Maximum Depth of Binary Tree

LeetCode 102_104_111_22_69_208_191_338_231_第4张图片

/**
 * 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;
        
    }
}

111. Minimum Depth of Binary Tree

LeetCode 102_104_111_22_69_208_191_338_231_第5张图片

/**
 * 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;
        
    }
}

22. Generate Parentheses

LeetCode 102_104_111_22_69_208_191_338_231_第6张图片
LeetCode 102_104_111_22_69_208_191_338_231_第7张图片
LeetCode 102_104_111_22_69_208_191_338_231_第8张图片
LeetCode 102_104_111_22_69_208_191_338_231_第9张图片
LeetCode 102_104_111_22_69_208_191_338_231_第10张图片
LeetCode 102_104_111_22_69_208_191_338_231_第11张图片
两道数独,两道N皇后

69. Sqrt(x)

LeetCode 102_104_111_22_69_208_191_338_231_第12张图片
二分法:

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)

LeetCode 102_104_111_22_69_208_191_338_231_第13张图片
LeetCode 102_104_111_22_69_208_191_338_231_第14张图片
LeetCode 102_104_111_22_69_208_191_338_231_第15张图片
LeetCode 102_104_111_22_69_208_191_338_231_第16张图片

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

212. Word Search II

LeetCode 102_104_111_22_69_208_191_338_231_第17张图片
LeetCode 102_104_111_22_69_208_191_338_231_第18张图片
LeetCode 102_104_111_22_69_208_191_338_231_第19张图片
LeetCode 102_104_111_22_69_208_191_338_231_第20张图片
LeetCode 102_104_111_22_69_208_191_338_231_第21张图片

191. Number of 1 Bits

LeetCode 102_104_111_22_69_208_191_338_231_第22张图片

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

LeetCode 102_104_111_22_69_208_191_338_231_第23张图片

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

LeetCode 102_104_111_22_69_208_191_338_231_第24张图片

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;
        
    }
}

你可能感兴趣的:(leetcode)