Leetcode之路3:Binary Tree Traversal、Two Sum

Binary Tree Postorder Traversal

Given a binary tree, return the postorder traversal of its nodes' values.

For example:
Given binary tree {1,#,2,3},
1

2
/
3
return [3,2,1].

簡單的題...直接上代碼

package binary.tree.postorder.traversal;

import java.util.ArrayList;
import java.util.List;

/*
 * 二叉樹的后續遍歷 
 * 沒有要求的話就用遞歸,應該很快
 * 如果他的左子葉為空,則訪問其右子葉,右子葉為空,訪問上級點
 * 如果其右子葉不為空,則遞歸進入
 */
public class Solution {
    
    private List<Integer> result;
    
    public Solution(){
        result = new ArrayList<Integer>();
    }
    public List<Integer> postorderTraversal(TreeNode root) {
        postorderTraversal2(root);
        return result;        
    }
    
    public void postorderTraversal2(TreeNode root){
        if(root == null)
            return ;    
        if(root.left != null){
            postorderTraversal2(root.left);
        }
        if(root.right != null){
            postorderTraversal2(root.right);
        }
        result.add(root.val);
    }
}

Binary Tree Preorder Traversal 也很簡單,實現了上面的這個也不難 不上代碼了

算法課上肯定會學的...憑印象自己實現了一下

Two Sum

Given an array of integers, find two numbers such that they add up to a specific target number.

The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.

You may assume that each input would have exactly one solution.

Output: index1=1, index2=2



輸入一個數組,找到 滿足兩數之和等于目標數的下標,返回

/*

  • 最簡單思路 掃描 超時了
    */
    public class Solution {
    public int[] twoSum(int[] numbers, int target) {
    int[] result = {0,0};
    for(int count1 = 0;count1 < numbers.length ; count1++){
    for(int count2 = count1;count2 < numbers.length;count2++){
    if(count1 == count2)
    continue;
    if(numbers[count1] + numbers[count2] == target){
    result[0] = count1;
    result[1] = count2;
    return result;
    }
    }
    }

     return null;
    

    }
    }


出現的問題在于要多次掃描  那么使用HashMap減少掃描

public class Solution {
int[] result = { 0 , 0 };
public int[] twoSum(int[] numbers, int target) {
Map<Integer,Integer> temp = new HashMap<Integer,Integer>();
for(int i = 0;i < numbers.length ; i++){
temp.put( target - numbers[i] , i);
}

    for(int i = 0;i < numbers.length ; i++){
        if(temp.containsKey(numbers[i])){
            if(i == temp.get(numbers[i])){
                continue;
            }else if(i > temp.get(numbers[i])){
                result[0] = temp.get(numbers[i]);
                result[1]  = i;
                return result;
            }else if(i < temp.get(numbers[i])){
                result[0] = i;
                result[1]  = temp.get(numbers[i]);
                return result;
            }
        }
    }
    return null;
}

public static void main(String[] args){
    Solution test = new Solution();
    int[] temp = {3,2,4};
    System.out.println(test.twoSum( temp, 6));
}

}


恩恩 通過...



最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容