binary tree
🎄树的数据结构
// Definition for a binary tree node.
public static 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;}
}
-
94 二叉树的中序遍历
package top.lel.lc.easy.binary_tree_inorder_traversal;import java.util.ArrayList; import java.util.List;/*** @author echo lovely* @date 2022/5/6 15:03* @description https://leetcode-cn.com/problems/binary-tree-inorder-traversal/*/public class BitreeInorderTraversal {public static void main(String[] args) {// 中序遍历顺序:左子树 根节点 右子树// [1, 0, 3, 2, 4]TreeNode treeNode = new TreeNode(0, new TreeNode(1), new TreeNode(2, new TreeNode(3), new TreeNode(4)));List<Integer> res = inorderTraversal(treeNode);System.out.println(res);}public static List<Integer> inorderTraversal(TreeNode root) {List<Integer> list = new ArrayList<>();forEach(list, root);return list;}private static void forEach(List<Integer> list, TreeNode treeNode) {if (treeNode == null) {return;}if (treeNode.left != null) {forEach(list, treeNode.left);}list.add(treeNode.val);if (treeNode.right != null) {forEach(list, treeNode.right);}}}
-
100 相同的树
深度搜索(递归),广度搜索(队列先进先去)package top.lel.lc.easy.same_tree;import java.util.LinkedList; import java.util.Queue;/*** @author echo lovely* @date 2022/5/6 17:20* @description 相同的树* https://leetcode-cn.com/problems/same-tree/* 1. 深度优先:递归* 2. 广度优先:使用队列*/public class SameTree {public static void main(String[] args) {/*boolean sameTree = isSameTree(new TreeNode(0, new TreeNode(), new TreeNode()), new TreeNode());System.out.println(sameTree);*/System.out.println(1 ^ 2);System.out.println(3 ^ 8 ^ 12);// 相同取0,相异取1/*System.out.println(true ^ false);System.out.println(false ^ true);System.out.println(true ^ true);System.out.println(false ^ false);*/boolean b = deepWay(new TreeNode(9, new TreeNode(0), new TreeNode(1)), new TreeNode(9, new TreeNode(0), new TreeNode(1)));System.out.println(b);}private static boolean deepWay(TreeNode p, TreeNode q) {if (p == null && q == null) {return true;}if (p == null || q == null) {return false;}Queue<TreeNode> queue1 = new LinkedList<>();queue1.offer(p);Queue<TreeNode> queue2 = new LinkedList<>();queue2.offer(q);while (!queue1.isEmpty() && !queue2.isEmpty()) {TreeNode treeNode1 = queue1.poll();TreeNode treeNode2 = queue2.poll();if (treeNode2 == null || (treeNode1.val != treeNode2.val)) {return false;}TreeNode left1 = treeNode1.left, left2 = treeNode2.left, right1 = treeNode1.right, right2 = treeNode2.right;// 异或:如果全部是空,判断是false, 全部不是空, 判断也是false. 则跳过if语句// 如果一个空,一个非空,return falseif (left1 == null ^ left2 == null) {return false;}if (right1 == null ^ right2 == null) {return false;}if (left1 != null) {queue1.offer(left1);}if (left2 != null) {queue2.offer(left2);}if (right1 != null) {queue1.offer(right1);}if (right2 != null) {queue2.offer(right2);}}return queue1.isEmpty() && queue2.isEmpty();}private static boolean isSameTree(TreeNode p, TreeNode q) {if (p == null && q == null) {return true;}if (p != null && q != null && p.val == q.val) {return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);}return false;}} }
-
101 对称二叉树
package top.lel.lc.easy.symmetric_tree;import java.util.LinkedList; import java.util.Queue;/*** @author echo lovely* @date 2022/5/7 13:46* @description 对称二叉树* https://leetcode-cn.com/problems/symmetric-tree/*/public class SymmetricTree {public static void main(String[] args) {boolean symmetric = isSymmetric(new TreeNode(99,new TreeNode(1, new TreeNode(2), new TreeNode(3)),new TreeNode(1, new TreeNode(3), new TreeNode(2))));System.out.println(symmetric);}public static boolean isSymmetric(TreeNode root) {if (root == null) {return false;}return forEach(root, root);}public static boolean forEach(TreeNode node0, TreeNode node1) {if (node0 == null && node1 == null) {return true;}if (node0 == null || node1 == null) {return false;}return node0.val == node1.val && forEach(node0.left, node1.right) && forEach(node0.right, node1.left);}public static boolean useQueue(TreeNode node0, TreeNode node1) {Queue<TreeNode> queue = new LinkedList<>();queue.offer(node0);queue.offer(node1);while (!queue.isEmpty()) {TreeNode n1 = queue.poll();TreeNode n2 = queue.poll();// 左子树搜索完 再搜索右子树if (n1 == null && n2 == null) {continue;}if (n1 == null || n2 == null) {return false;}if (n1.val != n2.val) {return false;}queue.offer(n1.left);queue.offer(n2.right);queue.offer(n1.right);queue.offer(n2.left);}return true;}// Definition for a binary tree node.public static 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;}}}
-
104 二叉树的高度
Max(leftH, rightH) + 1package top.lel.lc.easy.binary_tree_max_depth;import top.lel.lc.easy.tree.TreeNode;/*** @author echo lovely* @date 2022/5/7 14:58* @description 二叉树的最大深度* https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/*/public class MaxDepthOfBinaryTree {public static void main(String[] args) {}public int maxDepth(TreeNode root) {if (root == null) {return 0;}return getMaxDepth( root);}public int getMaxDepth(TreeNode node0) {if (node0 == null) {return 0;} else {int leftDepth = getMaxDepth(node0.left);int rightDepth = getMaxDepth(node0.right);System.out.println(leftDepth + "\t" + rightDepth);return Math.max(leftDepth, rightDepth) + 1;}}}