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;
        }
    }
}
相关推荐
TT哇几秒前
【每日八股】面经常考
java·面试
何中应几秒前
【面试题-4】JVM
java·jvm·后端·面试题
老毛肚1 分钟前
黑马头条-再回首
java
专注于大数据技术栈3 分钟前
java学习--8个包装类
java·学习
胖咕噜的稞达鸭6 分钟前
算法入门:专题前缀和:一二维前缀和 寻找数组的中心下标 除自身以外数组的乘积 和为k的子数组 和可被k整除的子数组 连续数组 矩阵区域和
线性代数·算法·矩阵
天赐学c语言6 分钟前
12.13 - 岛屿数量 && C语言中extern关键字的作用
c++·算法·leetcode
Lyinj7 分钟前
从一个编辑校验问题谈接口设计的边界
java·spring boot·python·学习
消失的旧时光-19437 分钟前
Java 线程通信:彻底理解 wait / notify(原理 + 图解 + 实战)
java·开发语言
徐子元竟然被占了!!8 分钟前
Linux-chown
java·linux·运维
AndrewHZ11 分钟前
【图像处理基石】如何入门图像金字塔算法技术?
图像处理·算法·计算机视觉·cv·拉普拉斯变换·图像金字塔