LeetCode解題(20200703)

劍指 Offer 07. 重建二叉樹

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
        //輸入的是前序遍歷的結果 中序遍歷的結果
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        //遞歸函數的出口是
        if (preorder == null || preorder.length == 0) {
            return null;
        }
        //獲取到根節點的value的值
        TreeNode root = new TreeNode(preorder[0]);
        //構建left right子樹
        int index = findIndex(preorder, inorder);
        // root.left = buildTree( 左子樹的前序數組 左子樹的中序數組);
        root.left = buildTree(Arrays.copyOfRange(preorder, 1, index+1), Arrays.copyOfRange(inorder, 0, index));
        //root.right = buildTree(右子樹的前序數組 右子樹的中序數組);
        root.right = buildTree(Arrays.copyOfRange(preorder, index + 1, preorder.length), Arrays.copyOfRange(inorder, index + 1, inorder.length));

        return root;
    }

    //爲了找到中序遍歷的過程中位置
    private int findIndex(int[] preorder, int[] inorder) {
        for (int i = 0; i < inorder.length; i++) {
            if (inorder[i] == preorder[0]) {
                return i;
            }
        }
        return 0;
    }
}

108. 將有序數組轉換爲二叉搜索樹

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode sortedArrayToBST(int[] nums) {
             if (nums == null) {
            return null;
        }
        TreeNode root = help(nums, 0, nums.length - 1);
        return root;
    }

    private TreeNode help(int[] nums, int left, int right) {
        if (left > right) {
            return null;
        }
        if (left == right) {
            return new TreeNode(nums[left]);
        }
        int mid = (left + right) / 2;
        TreeNode current = new TreeNode(nums[mid]);
        current.left = help(nums, left, mid - 1);
        current.right = help(nums, mid + 1, right);
        return current;
    }
}

劍指 Offer 26. 樹的子結構

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public boolean isSubStructure(TreeNode A, TreeNode B) {
        //遞歸出口
        if (A == null || B == null) {
            return false;
        }
        //遞歸函數
        return help(A, B) || isSubStructure(A.left, B) || isSubStructure(A.right, B);
    }

    private Boolean help(TreeNode A, TreeNode B) {
        //遞歸出口
        if (B == null) return true;
        //B已經不是空 但是A爲空的時候
        if (A == null) return false;
        //遞歸函數
        return A.val == B.val && help(A.left, B.left) && help(A.right, B.right);
    }
}

劍指 Offer 27. 二叉樹的鏡像

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode mirrorTree(TreeNode root) {
        if(root == null) return null;
        TreeNode tmp = root.left;
        root.left = mirrorTree(root.right);
        root.right = mirrorTree(tmp);
        return root;
    }
}

110. 平衡二叉樹

public class Solution {
    public boolean IsBalanced_Solution(TreeNode root) {
        return recur(root) != -1;
    }
    public int recur(TreeNode root) {
        if (root == null) return 0;
        int left = recur(root.left);
        if(left == -1) return -1;
        int right = recur(root.right);
        if(right == -1) return -1;
        return Math.abs(left - right) < 2 ? Math.max(left, right) + 1 : -1;
    }
}

 劍指 Offer 28. 對稱的二叉樹

class Solution {
    public boolean isSymmetric(TreeNode root) {
        return root == null ? true : recur(root.left, root.right);
    }
    boolean recur(TreeNode L, TreeNode R) {
        if(L == null && R == null) return true;
        if(L == null || R == null || L.val != R.val) return false;
        return recur(L.left, R.right) && recur(L.right, R.left);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章