标签:hex mod out 后序遍历 version next 后序 ram node
//1.------前中后序,递归,查找二叉树最大节点
/** 二叉树的最大节点 在二叉树中寻找值最大的节点并返回。 */ public class TreeNode { public int value; public TreeNode leftTreeNode; public TreeNode rightTreeNode; public TreeNode(int value, TreeNode leftTreeNode, TreeNode rightTreeNode) { this.value = value; this.leftTreeNode = leftTreeNode; this.rightTreeNode = rightTreeNode; } } private static TreeNode maxTreeNode; /** * 考点:二叉树-深度遍历-递归-前序遍历 * @param root * @return */ public TreeNode maxNodePre(TreeNode root) { if (root == null) { return null; } maxTreeNode = root; nextTreeNodePre(root); return maxTreeNode; } private void nextTreeNodePre(TreeNode treeNode) { System.out.println(treeNode.value); if (treeNode.value > maxTreeNode.value) { maxTreeNode = treeNode; } if (treeNode.leftTreeNode != null) { nextTreeNodePre(treeNode.leftTreeNode); } if (treeNode.rightTreeNode != null) { nextTreeNodePre(treeNode.rightTreeNode); } } /** * 考点:二叉树-深度遍历-递归-中序遍历 * @param root * @return */ public TreeNode maxNodeMid(TreeNode root) { if (root == null) { return null; } maxTreeNode = root; nextTreeNodeMid(root); return maxTreeNode; } private void nextTreeNodeMid(TreeNode treeNode) { if (treeNode.leftTreeNode != null) { nextTreeNodeMid(treeNode.leftTreeNode); } System.out.println(treeNode.value); if (treeNode.value > maxTreeNode.value) { maxTreeNode = treeNode; } if (treeNode.rightTreeNode != null) { nextTreeNodeMid(treeNode.rightTreeNode); } } /** * 考点:二叉树-深度遍历-递归-后序遍历 * @param root * @return */ public TreeNode maxNodeAfter(TreeNode root) { if (root == null) { return null; } maxTreeNode = root; nextTreeNodeAfter(root); return maxTreeNode; } private void nextTreeNodeAfter(TreeNode treeNode) { if (treeNode.leftTreeNode != null) { nextTreeNodeAfter(treeNode.leftTreeNode); } if (treeNode.rightTreeNode != null) { nextTreeNodeAfter(treeNode.rightTreeNode); } System.out.println(treeNode.value); if (treeNode.value > maxTreeNode.value) { maxTreeNode = treeNode; } }
//2----------------------------------------------------------------------------------------------- /** * 进制转换 给定一个十进制数 n 和 一个整数 k, 将 十进制数 n 转换成 k进制数. 【方法】:递归整除和余数 * @param n * @param k * @return */ public String hexConversion(int n, int k) { StringBuffer resultNumber = new StringBuffer(); tenToK(resultNumber, n, k); System.out.println("n:k:result: " + n +" "+ k + " " + resultNumber.toString()); return resultNumber.toString(); } private void tenToK(StringBuffer stringBuffer, int n, int k) { int integral = n/k; int mode = n % k; stringBuffer.insert(0, mode); if (integral >= k) { tenToK(stringBuffer, integral, k); } else if (integral > 0) { stringBuffer.insert(0, integral); } } private void testHexConversion() { hexConversion(3, 4); hexConversion(9, 4); hexConversion(29, 4); hexConversion(3, 5); hexConversion(9, 5); hexConversion(29, 5); }
//3----- 位运算实现加法
public int aplusb(int a, int b) { int sum_without_carry, carry; sum_without_carry = a^b; //没有进位的和 carry = (a&b)<<1; //进位 if(carry==0) return sum_without_carry; else return aplusb(sum_without_carry, carry); } public void testAplusB() { int result = aplusb(4,19); System.out.println(result); }
(二)3道简单算法:二叉树最大节点、十进制转任意进制、位运算实现加法
标签:hex mod out 后序遍历 version next 后序 ram node
原文地址:https://www.cnblogs.com/julychu89/p/9441785.html