目录
1. 二叉树的前序、中序和后序遍历
2. 二叉树的层序遍历
3. 二叉树的最大深度
4. 相同的树
5. 对称二叉树
6. 翻转二叉树
7. 左叶子之和
8. 平衡二叉树
9. 从前序与中序遍历序列构造二叉树
10. 二叉树的最小深度
11. 二叉树的直径
1. 二叉树的前序、中序和后序遍历
树的数据结构
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;
}
}
前序遍历 Leetcode144.二叉树的前序遍历
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
if (root == null) {
return list;
}
list.add(root.val);
list.addAll(inorderTraversal(root.left));
list.addAll(inorderTraversal(root.right));
return list;
}
}
中序遍历 Leetcode94.二叉树的中序遍历
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
if (root == null) {
return list;
}
list.addAll(inorderTraversal(root.left));
list.add(root.val);
list.addAll(inorderTraversal(root.right));
return list;
}
}
后序遍历 Leetcode145.二叉树的后序遍历
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
if (root == null) {
return list;
}
list.addAll(inorderTraversal(root.left));
list.addAll(inorderTraversal(root.right));
list.add(root.val);
return list;
}
}
2. 二叉树的层序遍历(BFS) Leetcode102.二叉树的层序遍历
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> lists = new ArrayList<List<Integer>>();
if (root == null) {
return lists;
}
Deque<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
int size = queue.size();
List<Integer> list = new ArrayList<>();
for (int i = 0; i < size; i++) {
TreeNode node = queue.poll();
if (node.left != null) {
queue.offer(node.left);
}
if (node.right != null) {
queue.offer(node.right);
}
list.add(node.val);
}
lists.add(list);
}
return lists;
}
}
3. 二叉树的最大深度 Leetcode104.二叉树的最大深度
class Solution {
public int maxDepth(TreeNode root) {
if (root == null) {
return 0;
}
return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
}
}
4. 相同的树 Leetcode100.相同的树
class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if (p == null && q == null) {
return true;
}
if (p == null || q == null || p.val != q.val) {
return false;
}
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
}
}
5. 对称二叉树 Leetcode101.对称二叉树
class Solution {
public boolean isSymmetric(TreeNode root) {
if (root == null) {
return true;
}
return check(root.left, root.right);
}
public boolean check(TreeNode p, TreeNode q) {
if (p == null && q == null) {
return true;
}
if (p == null || q == null || p.val != q.val) {
return false;
}
return check(p.left, q.right) && check(p.right, q.left);
}
}
6. 翻转二叉树 Leetcode226.翻转二叉树
class Solution {
public TreeNode invertTree(TreeNode root) {
if (root == null) {
return null;
}
// 从叶子节点先开始翻转
TreeNode left = invertTree(root.left);
TreeNode right = invertTree(root.right);
root.left = right;
root.right = left;
return root;
}
}
7. 左叶子之和 Leetcode404.左叶子之和
class Solution {
public int sumOfLeftLeaves(TreeNode root) {
if (root == null) {
return 0;
}
// 左子树为空
if (root.left == null) {
return sumOfLeftLeaves(root.right);
}
// 是左叶子节点
if (root.left.left == null && root.left.right == null) {
return root.left.val + sumOfLeftLeaves(root.right);
}
return sumOfLeftLeaves(root.left) + sumOfLeftLeaves(root.right);
}
}
8. 平衡二叉树 Leetcode.110平衡二叉树
class Solution {
public boolean isBalanced(TreeNode root) {
return height(root) >= 0;
}
public int height(TreeNode root) {
// 自底向上O(n)
if (root == null) {
return 0;
}
int m = height(root.left);
int n = height(root.right);
if (m == -1 || n == -1 || Math.abs(m - n) > 1) {
return -1;
}
return Math.max(m, n) + 1;
}
}
9. 从前序与中序遍历序列构造二叉树 Leetcode.105从前序与中序遍历序列构造二叉树
10. 二叉树的最小深度 Leetcode.111二叉树的最小深度
class Solution {
public int minDepth(TreeNode root) {
if (root == null) {
return 0;
}
// 叶子节点
if (root.left == null && root.right == null) {
return 1;
}
if (root.left == null) {
return minDepth(root.right) + 1;
}
if (root.right == null) {
return minDepth(root.left) + 1;
}
return Math.min(minDepth(root.left), minDepth(root.right)) + 1;
}
}
11. 二叉树的直径 Leetcode.543二叉树的直径
一棵二叉树的直径长度是任意两个结点路径长度中的最大值。
class Solution {
int ans = 0;
public int diameterOfBinaryTree(TreeNode root) {
height(root);
return ans;
}
/**
* 计算二叉树的深度
*/
public int height(TreeNode root) {
if (root == null) {
return 0;
}
// 左子树深度
int m = height(root.left);
// 右子树深度
int n = height(root.right);
// m + n 为当前子树的直径
ans = Math.max(ans, m + n);
return Math.max(m, n) + 1;
}
}