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;
        }
    }
}
相关推荐
REDcker9 分钟前
Linux C++ 内存泄漏排查分析手册
java·linux·c++
杰克尼11 分钟前
苍穹外卖--day11
java·数据库·spring boot·mybatis·notepad++
weixin1997010801613 分钟前
搜好货商品详情页前端性能优化实战
java·前端·python
XiYang-DING19 分钟前
【Java SE】Java代码块详解
java·开发语言·python
白云如幻20 分钟前
【JDBC】面向对象的思路编写JDBC程序
java·数据库
摇滚侠22 分钟前
Java SpringBoot 项目,项目启动后执行的方法,有哪些方式实现
java·开发语言·spring boot
哈哈很哈哈23 分钟前
逻辑回归Logistic Regression
算法·机器学习·逻辑回归
甄心爱学习29 分钟前
【极大似然估计/最大化后验】为什么逻辑回归要使用交叉熵损失函数
算法·机器学习·逻辑回归
艾莉丝努力练剑33 分钟前
【Linux进程间通信:共享内存】为什么共享内存的 key 值由用户设置
java·linux·运维·服务器·开发语言·数据库·mysql
郝学胜-神的一滴43 分钟前
深度学习入门全解析:从核心概念到实战基础 | 技术研讨会精华总结
人工智能·python·深度学习·算法·cnn