剑指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
- 剑指offer--例题
- 剑指offer 例题
- 剑指offer48相关例题
- 例题
- 例题
- 例题
- 例题
- 剑指OFFER
- 剑指offer
- 剑指Offer
- 剑指offer
- 剑指Offer
- 剑指offer
- 剑指offer
- 剑指offer
- 剑指Offer
- 剑指offer
- 剑指offer
- Android Handler Runnable和Thread之间的区别和联系详解
- 未能加载文件或程序集“XXXXXX”或它的某一个依赖项。试图加载格式不正确的程序
- 独立游戏demo-FoodStall3.0
- Android ListView中的项中包含RadioGroup时,RadioButton混乱选中的问题的解决方法
- 【SPOJ】 1043 Can you answer these queries I 线段树
- 剑指offer--例题
- Mapreduce计算框架涉及的技术(3)--以多视图方式看mapreduce
- vc实现控制台绘图
- TortoiseGit拉代码需要密码解决方法
- PID26 线性DP
- 迅雷近几年笔试题及其分析
- (白书训练计划)UVa 11572 Unique Snowflakes(窗口滑动法)
- 各大电商的缓存策略
- xcode配置boost