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;
        }
    }
}
相关推荐
枫叶林FYL38 分钟前
【群体智能集群控制工程实践】第3章 一致性协同算法
算法
禹凕1 小时前
滑动窗口算法实战指南
python·算法
evans在进步1 小时前
Tomcat NIO 工作原理详解:从 Acceptor、Poller 到容器处理链
java·tomcat·nio
DisonTangor1 小时前
【腾讯混元雪耻归来】 Hy4 preview:770B 参数 MoE 旗舰模型,1M 上下文全面开源
人工智能·算法·开源·aigc·腾讯云·腾讯云ai代码助手
明月_清风2 小时前
从二叉树到 B+ 树:一文搞懂工程中「树」的演化之道
数据结构·算法·go
渡我白衣2 小时前
并查集:基础认识与模拟实现
android·java·javascript·数据结构·c++·算法·并查集
电商API_180079052472 小时前
电商商品价格监控工具淘宝京东商品价格抓取API项目实操分享
java·大数据·开发语言·前端·数据挖掘
zzzll11112 小时前
深度剖析:HashMap 并发死循环与 ConcurrentHashMap 两代演进全解
java·开发语言
秋名RG3 小时前
2026/5/27 生产事故复盘与整改方案
java
hetao17338373 小时前
2026-09-01~09-04 hetao1733837 的刷题记录
c++·算法