1,题目:输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所 有路径。从树的根节点开始往下一直到叶结点所经过的结点形成一条路径。
public class TreeRoad { public static void main(String args[]) { BinaryTreeNode root1 = new BinaryTreeNode(); BinaryTreeNode node1 = new BinaryTreeNode(); BinaryTreeNode node2 = new BinaryTreeNode(); BinaryTreeNode node3 = new BinaryTreeNode(); BinaryTreeNode node4 = new BinaryTreeNode(); root1.leftNode = node1; root1.rightNode = node2; node1.leftNode = node3; node1.rightNode = node4; root1.value = 10; node1.value = 5; node2.value = 12; node3.value = 4; node4.value = 7; TreeRoad testFindPath = new TreeRoad(); testFindPath.findPath(root1, 22); } public void findPath(BinaryTreeNode root, int sum) { if (root == null) return; Stack<Integer> stack = new Stack<Integer>(); int currentSum = 0; findPath(root, sum, stack, currentSum); } private void findPath(BinaryTreeNode root, int sum, Stack<Integer> stack, int currentSum) { currentSum += root.value; stack.push(root.value); if (root.leftNode == null && root.rightNode == null) { //判断是否是叶子节点 if (currentSum == sum) { System.out.println("找到一个路径"); for (int path : stack) { System.out.print(path + " "); } System.out.println(); } } if (root.leftNode != null) { findPath(root.leftNode, sum, stack, currentSum); } if (root.rightNode != null) { findPath(root.rightNode, sum, stack, currentSum); } stack.pop(); } }
public class BinaryTreeNode { public int value; public BinaryTreeNode leftNode; public BinaryTreeNode rightNode; public BinaryTreeNode(){ } }
2,给定数组,判断是否是二叉搜索树的后续遍历?
二叉查找树(又称二叉排序树)
(1) 它或者是一棵空树;
(2) 或者是具有下列性质的二叉树:
<1> 若左子树不空,则左子树上所有结点的值均小于它的根结点的值;
<2> 若右子树不空,则右子树上所有结点的值均大于它的根结点的值;
<3> 左、右子树也分别为二查找序树;
public class JudgePostSortTree { public static boolean judgePostSortTree(int[] arr) { if (arr == null && arr.length <= 0) { return false; } int length = arr.length; int i = 0; int root = arr[length - 1]; for (; i < length - 1; i++) { if (arr[i] > root) break; } int j = i; for (; j < length - 1; j++) { if (arr[j] < root) return false; } boolean leftFlag = true; if (i > 0) { leftFlag = judgePostSortTree(Arrays.copyOfRange(arr, 0, i)); } boolean rightFlag = true; if (i < arr.length - 1) { rightFlag = judgePostSortTree(Arrays.copyOfRange(arr, i, arr.length - 1)); } return leftFlag && rightFlag; } public static void main(String[] args) { int[] arr = new int[]{5, 7, 6, 9, 11, 10, 8}; boolean flag = judgePostSortTree(arr); System.out.println(flag); } }
3,从上往下打印二叉树的每个结点,同一层的结点按照从左到右的顺序打印。
public class LevelPrintTree { public static void main(String args[]) { BinaryTreeNode root1 = new BinaryTreeNode(); BinaryTreeNode node1 = new BinaryTreeNode(); BinaryTreeNode node2 = new BinaryTreeNode(); BinaryTreeNode node3 = new BinaryTreeNode(); BinaryTreeNode node4 = new BinaryTreeNode(); root1.leftNode = node1; root1.rightNode = node2; node1.leftNode = node3; node1.rightNode = node4; root1.value = 10; node1.value = 5; node2.value = 12; node3.value = 4; node4.value = 7; LevelPrintTree levelPrintTree = new LevelPrintTree(); levelPrintTree.levelPrintTree(root1); } public void levelPrintTree(BinaryTreeNode root) { if(root==null){ return; } Queue<BinaryTreeNode> queue=new LinkedList<BinaryTreeNode>(); queue.add(root); while (!queue.isEmpty()){ BinaryTreeNode node=queue.poll(); System.out.println(node.value); if(node.leftNode!=null){ queue.add(node.leftNode); } if(node.rightNode!=null){ queue.add(node.rightNode); } } } }
4,单链表的逆置
public static Node reverse(MyNode node){ //单链表为空或只有头结点或只有一个元素,不用进行逆置操作 if(node==null||node.next==null||node.next.next==null) return; //令p指向线性表中第2个元素a2 MyNode p=node.next.next; //令线性表中第1个元素a1的next为空 node.next.next=null; while(p!=null){ MyNode q=p.next; //将p插入头结点之后 p.next=node.next; node.next=p; p=q;//继续访问下一个元素 } return ; }
5,二叉树的镜像
请完成一个函数,输入一个二叉树,该函数输出它的镜像。
public class MirrorBinaryTree { public static void main(String[] args) { BinaryTreeNode root1=new BinaryTreeNode(); BinaryTreeNode node1=new BinaryTreeNode(); BinaryTreeNode node2=new BinaryTreeNode(); BinaryTreeNode node3=new BinaryTreeNode(); BinaryTreeNode node4=new BinaryTreeNode(); BinaryTreeNode node5=new BinaryTreeNode(); BinaryTreeNode node6=new BinaryTreeNode(); root1.leftNode=node1; root1.rightNode=node2; node1.leftNode=node3; node1.rightNode=node4; node4.leftNode=node5; node4.rightNode=node6; root1.value=8; node1.value=8; node2.value=7; node3.value=9; node4.value=2; node5.value=4; node6.value=7; MirrorBinaryTree test=new MirrorBinaryTree(); BinaryTreeNode rootBinaryTreeNode=test.mirrorBinaryTree(root1); System.out.println(root1.rightNode.value); } public BinaryTreeNode mirrorBinaryTree(BinaryTreeNode root){ if(root==null){ return null; } if(root.leftNode==null&&root.rightNode==null) return null; Stack<BinaryTreeNode> stack=new Stack<BinaryTreeNode>(); while(root!=null||!stack.isEmpty()){ while(root!=null){ BinaryTreeNode temp=root.leftNode; root.leftNode=root.rightNode; root.rightNode=temp; stack.push(root); root=root.leftNode; } root=stack.pop(); root=root.rightNode; } return root; } }