1. 程式人生 > 其它 >力扣104:二叉樹的最大深度

力扣104:二叉樹的最大深度

技術標籤:力扣

力扣104:二叉樹的最大深度

解題思路
1、標籤:DFS
2、找出終止條件:當前節點為空
3、找出返回值:節點為空時說明高度為 0,所以返回 0;節點不為空時則分別求左右子樹的高度的最大值,同時加1表示當前節點的高度,返回該數值
4、某層的執行過程:在返回值部分基本已經描述清楚
5、時間複雜度:O(n)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 int maxDepth(TreeNode root) { if(root == null) { return 0; } else { int left = maxDepth(root.left); int right = maxDepth(root.right); return Math.max(left, right) + 1; } } }

力扣226:翻轉二叉樹

方法一:遞迴
思路與演算法

這是一道很經典的二叉樹問題。顯然,我們從根節點開始,遞迴地對樹進行遍歷,並從葉子結點先開始翻轉。如果當前遍歷到的節點 \textit{root}root 的左右兩棵子樹都已經翻轉,那麼我們只需要交換兩棵子樹的位置,即可完成以 \textit{root}root 為根節點的整棵子樹的翻轉。

程式碼

class Solution {
    public TreeNode invertTree(TreeNode root) {
        if (root == null) {
            return null;
        }
        TreeNode left =
invertTree(root.left); TreeNode right = invertTree(root.right); root.left = right; root.right = left; return root; } }
其實就是交換一下左右節點,然後再遞迴的交換左節點,右節點
根據動畫圖我們可以總結出遞迴的兩個條件如下:

終止條件:當前節點為 null 時返回
交換當前節點的左右節點,再遞迴的交換當前節點的左節點,遞迴的交換當前節點的右節點
時間複雜度:每個元素都必須訪問一次,所以是 O(n)O(n)
空間複雜度:最壞的情況下,需要存放 O(h)O(h) 個函式呼叫(h是樹的高度),所以是 O(h)O(h)
程式碼實現如下:
class Solution {
	public TreeNode invertTree(TreeNode root) {
		//遞迴函式的終止條件,節點為空時返回
		if(root==null) {
			return null;
		}
		//下面三句是將當前節點的左右子樹交換
		TreeNode tmp = root.right;
		root.right = root.left;
		root.left = tmp;
		//遞迴交換當前節點的 左子樹
		invertTree(root.left);
		//遞迴交換當前節點的 右子樹
		invertTree(root.right);
		//函式返回時就表示當前這個節點,以及它的左右子樹
		//都已經交換完了
		return root;
	}
}


力扣234:迴文連結串列

請判斷一個連結串列是否為迴文連結串列。

示例 1:

輸入: 1->2
輸出: false
示例 2:

輸入: 1->2->2->1
輸出: true
進階:
你能否用 O(n) 時間複雜度和 O(1) 空間複雜度解決此題?

2,使用棧解決
我們知道棧是先進後出的一種資料結構,這裡還可以使用棧先把連結串列的節點全部存放到棧中,然後再一個個出棧,這樣就相當於連結串列從後往前訪問了,通過這種方式也能解決,看下程式碼


public boolean isPalindrome(ListNode head) {
    ListNode temp = head;
    Stack<Integer> stack = new Stack();
    //把連結串列節點的值存放到棧中
    while (temp != null) {
        stack.push(temp.val);
        temp = temp.next;
    }

    //然後再出棧
    while (head != null) {
        if (head.val != stack.pop()) {
            return false;
        }
        head = head.next;
    }
    return true;
}
這裡相當於連結串列從前往後全部都比較了一遍,其實我們只需要拿連結串列的後半部分和前半部分比較即可,沒必要全部比較,所以這裡可以優化一下


public boolean isPalindrome(ListNode head) {
    if (head == null)
        return true;
    ListNode temp = head;
    Stack<Integer> stack = new Stack();
    //連結串列的長度
    int len = 0;
    //把連結串列節點的值存放到棧中
    while (temp != null) {
        stack.push(temp.val);
        temp = temp.next;
        len++;
    }
    //len長度除以2
    len >>= 1;
    //然後再出棧
    while (len-- >= 0) {
        if (head.val != stack.pop())
            return false;
        head = head.next;
    }
    return true;
}

力扣448:找到所有陣列中消失的數字

給定一個範圍在 1 ≤ a[i] ≤ n ( n = 陣列大小 ) 的 整型陣列,陣列中的元素一些出現了兩次,另一些只出現一次。

找到所有在 [1, n] 範圍之間沒有出現在陣列中的數字。

您能在不使用額外空間且時間複雜度為O(n)的情況下完成這個任務嗎? 你可以假定返回的陣列不算在額外空間內。

示例:

輸入:
[4,3,2,7,8,2,3,1]

輸出:
[5,6]

class Solution {
    public List<Integer> findDisappearedNumbers(int[] nums) {
        // Hash table for keeping track of the numbers in the array
        // Note that we can also use a set here since we are not 
        // really concerned with the frequency of numbers.
        HashMap<Integer, Boolean> hashTable = new HashMap<Integer, Boolean>();
        
        // Add each of the numbers to the hash table
        for (int i = 0; i < nums.length; i++) {
            hashTable.put(nums[i], true);
        }
        
        // Response array that would contain the missing numbers
        List<Integer> result = new LinkedList<Integer>();
        
        // Iterate over the numbers from 1 to N and add all those
        // that don't appear in the hash table. 
        for (int i = 1; i <= nums.length; i++) {
            if (!hashTable.containsKey(i)) {
                result.add(i);
            }
        }
        
        return result;
    }
}

時間複雜度:O(N)
空間複雜度:O(N)