Java算法 leetcode简单【树的遍历,深度计算及比较】刷题记录

Java算法 leetcode简单【树的遍历,深度计算及比较】刷题记录

  1. 俩数之和: https://leetcode.cn/problems/two-sum/
  2. 二进制求和: https://leetcode.cn/problems/add-binary/
java 复制代码
class Solution {
    public String addBinary(String a, String b) {
        int numA = a.length();
        int numB = b.length();
        String s = "";
        if (numA > numB) {
            for (int i = 0; i < numA - numB; i++) {
                s += "0";
            }
            b = s + b;
        } else {
            for (int i = 0; i < numB - numA; i++) {
                s += "0";
            }
            a = s + a;
            // System.out.println(a+" "+b);
            String temp = a;
            a = b;
            b = temp;
        }
        StringBuilder sb = new StringBuilder();
        int cnt = 0;
        for (int j = a.length() - 1; j >= 0; j--) {
            int num = Integer.parseInt(a.charAt(j) + "") + Integer.parseInt(b.charAt(j) + "") + cnt;
            if (num >= 2) {
                sb.append(num % 2);
                cnt = 1;
            } else {
                cnt = 0;
                sb.append(num);
            }
        }
        if (cnt > 0) {
            sb.append(cnt);
        }
        return sb.reverse().toString();
    }
}
  1. 二叉树的中序遍历: https://leetcode.cn/problems/binary-tree-inorder-traversal/
  2. 二叉树的后序遍历: https://leetcode.cn/problems/binary-tree-postorder-traversal/
  3. 二叉树的前序遍历: https://leetcode.cn/problems/binary-tree-preorder-traversal/
    中序遍历(左根右)、前序遍历(根左右)、后序遍历(左右根)说的是根的相对位置;
java 复制代码
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 * int val;
 * TreeNode left;
 * TreeNode right;
 * TreeNode() {}
 * TreeNode(int val) { this.val = val; }
 * TreeNode(int val, TreeNode left, TreeNode right) {
 * this.val = val;
 * this.left = left;
 * this.right = right;
 * }
 * }
 */
class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        middleIterate(list, root);
        return list;
    }

    public void middleIterate(List<Integer> list, TreeNode root) {
        if (root == null) {
            return;
        }
        
        // 中序遍历 左根右
        middleIterate(list, root.left);
        list.add(root.val);
        middleIterate(list, root.right);
    }
}
  1. 相同的树: https://leetcode.cn/problems/same-tree/

    中序遍历对比

java 复制代码
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 * int val;
 * TreeNode left;
 * TreeNode right;
 * TreeNode() {}
 * TreeNode(int val) { this.val = val; }
 * TreeNode(int val, TreeNode left, TreeNode right) {
 * this.val = val;
 * this.left = left;
 * this.right = right;
 * }
 * }
 */
class Solution {
    public boolean isSameTree(TreeNode p, TreeNode q) {
        if (p != null && q != null) {
            if (p.val == q.val) {
                if (isSameTree(p.left, q.left)) {
                    return isSameTree(p.right, q.right);
                } else {
                    return false;
                }
            }
            return false;
        } else if (p == null && q == null) {
            return true;
        } else if (q == null) {
            return false;
        }
        return false;
    }
}
  1. 二叉树的最大深度: https://leetcode.cn/problems/maximum-depth-of-binary-tree/
java 复制代码
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 * int val;
 * TreeNode left;
 * TreeNode right;
 * TreeNode() {}
 * TreeNode(int val) { this.val = val; }
 * TreeNode(int val, TreeNode left, TreeNode right) {
 * this.val = val;
 * this.left = left;
 * this.right = right;
 * }
 * }
 */
class Solution {
    
    public int maxDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if (root.left != null || root.right != null) {
            return Math.max(1 + maxDepth(root.left), 1 + maxDepth(root.right));
        } else {
            return 1;
        }
    }
}
相关推荐
黑龙江亿林等保几秒前
深入探索哈尔滨二级等保下的负载均衡SLB及其核心算法
运维·算法·负载均衡
起名字真南3 分钟前
【OJ题解】C++实现字符串大数相乘:无BigInteger库的字符串乘积解决方案
开发语言·c++·leetcode
lucy153027510793 分钟前
【青牛科技】GC5931:工业风扇驱动芯片的卓越替代者
人工智能·科技·单片机·嵌入式硬件·算法·机器学习
爬山算法8 分钟前
Maven(28)如何使用Maven进行依赖解析?
java·maven
杜杜的man19 分钟前
【go从零单排】迭代器(Iterators)
开发语言·算法·golang
2401_8574396932 分钟前
SpringBoot框架在资产管理中的应用
java·spring boot·后端
怀旧66634 分钟前
spring boot 项目配置https服务
java·spring boot·后端·学习·个人开发·1024程序员节
李老头探索36 分钟前
Java面试之Java中实现多线程有几种方法
java·开发语言·面试
小沈熬夜秃头中୧⍤⃝36 分钟前
【贪心算法】No.1---贪心算法(1)
算法·贪心算法
芒果披萨41 分钟前
Filter和Listener
java·filter