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

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

  1. 俩数之和: https://leetcode.cn/problems/two-sum/
  2. 二进制求和: https://leetcode.cn/problems/add-binary/
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/
    中序遍历(左根右)、前序遍历(根左右)、后序遍历(左右根)说的是根的相对位置;

/**
 * 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/

    中序遍历对比

/**
 * 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/
/**
 * 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;
        }
    }
}

你可能感兴趣的:(JAVA,算法,算法,java,leetcode)