树的各种操作

目录

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;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值