剑指offer--例题

来源:互联网 发布:淘宝聚沙塔 编辑:程序博客网 时间:2024/06/06 12:33

最近准备校招的找工作,在看剑指offer,选做了一些面试题,记录一下而已。


面试题25 二叉树中和为某一值的路径

public class Test {    public static void find(BTreeNode root, int target) {        Stack<Integer> record = new Stack<Integer>();        if(root==null)            return;        help(root, target, 0, record);    }    private static void help(BTreeNode root, int target, int total, Stack<Integer> record) {        record.add(root.val);        if (root.left == null && root.right == null) {            if ((total + root.val) == target) {                for (int i = 0; i < record.size(); i++) {                    System.out.print(record.get(i)+"    ");                }                System.out.println();            }        } else {            if (root.left != null)                help(root.left, target, total + root.val, record);            if (root.right != null)                help(root.right, target, total + root.val, record);        }        record.pop();    }    public static void main(String[] args) {        BTreeNode a1 = new BTreeNode(10);        BTreeNode a2 = new BTreeNode(5);        BTreeNode a3 = new BTreeNode(12);        BTreeNode a4 = new BTreeNode(4);        BTreeNode a5 = new BTreeNode(7);        a1.left = a2;        a1.right = a3;        a2.left = a4;        a2.right = a5;        Test.find(a1, 52);    }}

面试题27 二叉搜索树转为有序双向链表

思路:递归、分治。对于每一个root节点,先将左子节点部分转为有序双两链表,再将又子节点部分转为有序双向链表。然后将root和两个链表拼接起来即可。

public class Test {    public static BTreeNode find(BTreeNode root) {        BTreeNode rs = root;        if (root == null)            return null;        while (rs.left != null) {            rs = rs.left;        }        change(root);        return rs;    }    private static void change(BTreeNode root) {        if (root != null) {            BTreeNode left = root.left;            BTreeNode leftRight = left;            BTreeNode right = root.right;            BTreeNode rightLeft = right;            if (leftRight != null) {                while (leftRight.right != null)                    leftRight = leftRight.right;            }            if (rightLeft != null) {                while (rightLeft.left != null)                    rightLeft = rightLeft.left;            }            change(left);            change(right);            if (leftRight != null) {                leftRight.right = root;                root.left = leftRight;            }            if (rightLeft != null) {                rightLeft.left = root;                root.right = rightLeft;            }        }    }    public static void main(String[] args) {        BTreeNode a1 = new BTreeNode(10);        BTreeNode a2 = new BTreeNode(6);        BTreeNode a3 = new BTreeNode(14);        BTreeNode a4 = new BTreeNode(4);        BTreeNode a5 = new BTreeNode(8);        BTreeNode a6 = new BTreeNode(12);        BTreeNode a7 = new BTreeNode(16);        a1.left = a2;        a1.right = a3;        a2.left = a4;        a2.right = a5;        a3.left = a6;        a3.right = a7;        BTreeNode rs = Test.find(a1);        System.out.println(rs);    }}


面试题28 字符串的全排列

栈、递归、深度优先。自己实现的和书上的不太一样。

import java.util.Stack;public class Test {    public void sort(char[] source) {        if (source == null || source.length <= 0)            return;        else {            boolean[] record = new boolean[source.length];            for (int i = 0; i < source.length; i++)                record[i] = false;            subSort(source, record, new Stack<Character>());        }    }    private void subSort(char[] source, boolean[] record, Stack<Character> stack) {        if (stack.size() == record.length)            System.out.println(stack);        else {            int i = 0;            for (; i < record.length; i++) {                if (record[i] == false) {                    stack.push(source[i]);                    record[i] = true;                    subSort(source, record, stack);                    stack.pop();                    record[i] = false;                }            }        }    }    public static void main(String[] args) {        new Test().sort(new char[]{'1', '2', '3', '4'});    }}


面试题39 判断是否为平衡二叉树

给定根节点。方法:后序遍历、记录每个节点的深度(深度肯定是大于等于0的,如果是-1,表示以该节点为根的子树不平衡)

public class Test {    public boolean isBalance(BTreeNode root) {        if (root == null)            return true;        else            return depthOfNode(root) == -1 ? false : true;    }    private int depthOfNode(BTreeNode node) {        if (node == null)            return 0;        else {            int leftDepth = node.left == null ? 0 : depthOfNode(node.left);            int rightDepth = node.right == null ? 0 : depthOfNode(node.right);            if (leftDepth == -1 || rightDepth == -1)                return -1;            else if (Math.abs(leftDepth - rightDepth) < 2)                return Math.max(leftDepth, rightDepth) + 1;            else                return -1;        }    }    public static void main(String[] args) throws Exception {        BTreeNode n1 = new BTreeNode(1);        BTreeNode n2 = new BTreeNode(2);        BTreeNode n3 = new BTreeNode(3);//        BTreeNode n4 = new BTreeNode(4);//        BTreeNode n5 = new BTreeNode(5);//        BTreeNode n6 = new BTreeNode(6);        n1.left = n2;//        n1.right = n3;        System.out.println(new Test().isBalance(n1));    }}


0 0
原创粉丝点击