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

Posted julychu89

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了3道简单算法:二叉树最大节点十进制转任意进制位运算实现加法相关的知识,希望对你有一定的参考价值。

    //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道简单算法:二叉树最大节点十进制转任意进制位运算实现加法的主要内容,如果未能解决你的问题,请参考以下文章

十进制转任意进制数

十进制转任意进制

每日算法-二叉树的最大深度(树-简单)

⭐算法入门⭐《二叉树》简单04 —— LeetCode 104. 二叉树的最大深度

校招的常考算法类型以及对应的典型题目

进制转换