码迷,mamicode.com
首页 > 编程语言 > 详细

(二)3道简单算法:二叉树最大节点、十进制转任意进制、位运算实现加法

时间:2018-08-08 12:02:56      阅读:188      评论:0      收藏:0      [点我收藏+]

标签: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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!