1. 程式人生 > >LeetCode095——不同的二叉搜尋樹 II

LeetCode095——不同的二叉搜尋樹 II

題目描述:

知識點:樹、遞迴

思路:根據二叉搜尋樹的定義遞迴實現

遞迴終止條件

如果傳遞進遞迴函式的arrayList中只有一個元素,顯然,該二叉樹列表中只有一顆樹,這棵樹只有一個根結點,其值為arrayList中唯一元素的值。

遞迴過程

遍歷arrayList中的所有值,將其分成小於當前節點的一部分less和大於當前節點的一部分greater。

(1)如果less和greater均至少含有一個元素,則我們分別遞迴呼叫得到其左子樹列表和右子樹列表。

(2)如果less和greater中有一個沒有元素,則我們將相應的左子樹或右子樹置為null。

需要注意的是:

tempNode,即遞迴過程中根結點的定義需要在遍歷左子樹列表leftTreeNodeList和右子樹列表rightTreeNodeList時定義。

時間複雜度是O(nlogn)。空間複雜度即遞迴深度,是O(logn)。

JAVA程式碼:

public class Solution {

    public List<TreeNode> generateTrees(int n) {
        ArrayList<Integer> arrayList = new ArrayList<>();
        for (int i = 1; i <= n; i++) {
            arrayList.add(i);
        }
        List<TreeNode> list = generateTrees(arrayList);
        return list;
    }

    private List<TreeNode> generateTrees(ArrayList<Integer> arrayList) {
        List<TreeNode> treeNodeList = new ArrayList<>();
        if (arrayList.size() == 1) {
            TreeNode treeNode = new TreeNode(arrayList.get(0));
            treeNodeList.add(treeNode);
            return treeNodeList;
        }
        for (int i = 0; i < arrayList.size(); i++) {
            ArrayList<Integer> less = new ArrayList<>();
            ArrayList<Integer> greater = new ArrayList<>();
            for (int j = 0; j < arrayList.size(); j++) {
                if (i == j) {
                    continue;
                }
                if (arrayList.get(j) < arrayList.get(i)) {
                    less.add(arrayList.get(j));
                } else {
                    greater.add(arrayList.get(j));
                }
            }
            if (less.size() != 0 && greater.size() != 0) {
                List<TreeNode> leftTreeNodeList = generateTrees(less);
                List<TreeNode> rightTreeNodeList = generateTrees(greater);
                for (TreeNode leftTreeNode : leftTreeNodeList) {
                    for (TreeNode rightTreeNode : rightTreeNodeList) {
                        TreeNode tempNode = new TreeNode(arrayList.get(i));
                        tempNode.left = leftTreeNode;
                        tempNode.right = rightTreeNode;
                        treeNodeList.add(tempNode);
                    }
                }
            }else if(less.size() == 0 && greater.size() != 0){
                List<TreeNode> rightTreeNodeList = generateTrees(greater);
                for (TreeNode rightTreeNode : rightTreeNodeList) {
                    TreeNode tempNode = new TreeNode(arrayList.get(i));
                    tempNode.left = null;
                    tempNode.right = rightTreeNode;
                    treeNodeList.add(tempNode);
                }
            }else if(less.size() != 0 && greater.size() == 0){
                List<TreeNode> leftTreeNodeList = generateTrees(less);
                for (TreeNode leftTreeNode : leftTreeNodeList) {
                    TreeNode tempNode = new TreeNode(arrayList.get(i));
                    tempNode.left = leftTreeNode;
                    tempNode.right = null;
                    treeNodeList.add(tempNode);
                }
            }
        }
        return treeNodeList;
    }
}

LeetCode解題報告: