左神第四课

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/BuZiShuoquoto/article/details/81203864

第一题

实现二叉树的先序、中序、后序遍历,包括递归方式和非递归方式

第二题

如何直观的打印一颗二叉树

解析
为了能够更加直观的看见一颗二叉树而设计的程序

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为这棵树的节点个数

猜你喜欢

转载自blog.csdn.net/BuZiShuoquoto/article/details/81203864