1. 程式人生 > >左神第四課

左神第四課

第一題

實現二叉樹的先序、中序、後序遍歷,包括遞迴方式和非遞迴方式

第二題

如何直觀的列印一顆二叉樹

解析
為了能夠更加直觀的看見一顆二叉樹而設計的程式

public class Code_02_PrintBinaryTree {

    public static class Node {
        public int value;
        public Node left;
        public Node right;

        public Node(int data) {
            this.value = data;
        }
    }

    public
static void printTree(Node head) { System.out.println("Binary Tree:"); printInOrder(head, 0, "H", 17); System.out.println(); } public static void printInOrder(Node head, int height, String to, int len) { if (head == null) { return; } printInOrder(head.right
, height + 1, "v", len); String val = to + head.value + to; int lenM = val.length(); int lenL = (len - lenM) / 2; int lenR = len - lenM - lenL; val = getSpace(lenL) + val + getSpace(lenR); System.out.println(getSpace(height * len) + val); printInOrder(head.left
, height + 1, "^", len); } public static String getSpace(int num) { String space = " "; StringBuffer buf = new StringBuffer(""); for (int i = 0; i < num; i++) { buf.append(space); } return buf.toString(); } public static void main(String[] args) { Node head = new Node(1); head.left = new Node(-222222222); head.right = new Node(3); head.left.left = new Node(Integer.MIN_VALUE); head.right.left = new Node(55555555); head.right.right = new Node(66); head.left.left.right = new Node(777); printTree(head); head = new Node(1); head.left = new Node(2); head.right = new Node(3); head.left.left = new Node(4); head.right.left = new Node(5); head.right.right = new Node(6); head.left.left.right = new Node(7); printTree(head); head = new Node(1); head.left = new Node(1); head.right = new Node(1); head.left.left = new Node(1); head.right.left = new Node(1); head.right.right = new Node(1); head.left.left.right = new Node(1); printTree(head); } }

第三題

在二叉樹中找到一個節點的後繼節點
現在有一種新的二叉樹節點型別如下:

public class Node { 
    public int value; 
    public Node left;
    public Node right; 
    public Node parent;
    public Node(int data) { this.value = data; }
}

該結構比普通二叉樹節點結構多了一個指向父節點的parent指標。假設有一 棵Node型別的節點組成的二叉樹,樹中每個節點的parent指標都正確地指向 自己的父節點,頭節點的parent指向null。只給一個在二叉樹中的某個節點 node,請實現返回node的後繼節點的函式。在二叉樹的中序遍歷的序列中, node的下一個節點叫作node的後繼節點。

題目四

介紹二叉樹的序列化和反序列化

題目五

摺紙問題
請把一段紙條豎著放在桌子上,然後從紙條的下邊向上方對摺1次,壓出摺痕後展開。此時 摺痕是凹下去的,即摺痕突起的方向指向紙條的背面。如果從紙條的下邊向上方連續對摺2 次,壓出摺痕後展開,此時有三條摺痕,從上到下依次是下摺痕、下摺痕和上摺痕。給定一 個輸入引數N,代表紙條都從下邊向上方連續對摺N次,請從上到下列印所有摺痕的方向。
例如:N=1時,列印: down
N=2時,列印: down down up
N=3時,列印: down down up down down up up
N=4時,列印: down down up down down up up down down down up up down up up

解答
這裡寫圖片描述
當遍歷到右邊子樹時,需要標記打印出up,這和中序遍歷類似。如果不理解可以使用前序遍歷和後續遍歷。

public class Code_05_PaperFolding {

    public static void printAllFolds(int N) {
        printProcess(N, true);
    }

    public static void printProcess(int N, boolean down) {
        if (N<=0) {
            return;
        }
        printProcess(N-1, true);
        System.out.println(down ? "down " : "up ");
        printProcess(N-1, false);
    }

    public static void main(String[] args) {
        int N = 3;
        printAllFolds(N);
    }
}

題目六

判斷一棵二叉樹是否是平衡二叉樹

解析

分治法

public class Test2 {
    class ReturnType{
        private int high;
        private boolean isBalance;

        public ReturnType() {
            super();
        }

        public ReturnType(int high, boolean isBalance) {
            this.high = high;
            this.isBalance = isBalance;
        }

        public int getHigh() {
            return high;
        }

        public void setHigh(int high) {
            this.high = high;
        }

        public boolean isBalance() {
            return isBalance;
        }

        public void setBalance(boolean isBalance) {
            this.isBalance = isBalance;
        }

    }
    public boolean isBalanced(TreeNode root) {
        return isBlanced2(root).isBalance();

    }
    public ReturnType isBlanced2(TreeNode root){
        if (root==null) {
            return new ReturnType(0, true);
        }
        ReturnType left=isBlanced2(root.left);
        ReturnType right=isBlanced2(root.right);
        ReturnType r=new ReturnType();

        if (left.isBalance&&right.isBalance) {
            r.setBalance(Math.abs(left.high-right.high)<=1);
            r.setHigh(Math.max(left.high, right.high)+1);
            return r;
        }
        return new ReturnType(-1, false);
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub

    }

}

題目七

判斷一棵樹是否是搜尋二叉樹、判斷一棵樹是否是完全二叉樹

解析

這裡寫圖片描述
利用了二叉樹的前序遍歷的順序和中序遍歷的順序,在前序遍歷時,獲得右子樹的值,中序遍歷獲得左子樹的值,然後在後續遍歷中二者比較。
分治法也是可以的。

class Solution {
    public boolean isValidBST(TreeNode root) {
        return isValidBST(root, Long.MIN_VALUE, Long.MAX_VALUE);
    }

    private boolean isValidBST(TreeNode root, long minVal, long maxVal)
        {
            if (root == null) {
            return true;
        }
        int c=root.val;
        boolean a = isValidBST(root.left, minVal, c);
        int d=root.val;
        boolean b = isValidBST(root.right, d, maxVal);
        if (root.val >= maxVal || root.val <= minVal) {
            return false;
        }
        return a && b;
        }
}

題目八

已知一棵完全二叉樹,求其節點的個數
要求:時間複雜度低於O(N),N為這棵樹的節點個數