一、前序遍历相关

二、中序遍历相关

题解报告LeetCode#230:二叉搜索树中第 K 小的元素

题意:LeetCode#230:二叉搜索树中第 K 小的元素

思路:中序遍历一套带走

代码:

  1. class Solution {
  2. int count, res;
  3. public int kthSmallest(TreeNode root, int k) {
  4. if (root.left != null) kthSmallest(root.left, k);
  5. if (++count == k) res = root.val; // 注意不能在这里返回 // 妈的,可以在这里返回
  6. if (root.right != null) kthSmallest(root.right, k);
  7. return res;
  8. }
  9. }

题解报告LeetCode#530:二叉搜索树的最小绝对差

题意:LeetCode#530:二叉搜索树的最小绝对差

思路:中序遍历一波带走

代码:

  1. class Solution {
  2. private int result = Integer.MAX_VALUE;
  3. private TreeNode preNode = null;
  4. public int getMinimumDifference(TreeNode root) {
  5. getMin(root);
  6. return result;
  7. }
  8. private void getMin(TreeNode root) {
  9. if (root == null) {
  10. return;
  11. }
  12. getMin(root.left);
  13. if (preNode != null) {
  14. result = Math.min(Math.abs(root.val - preNode.val), result);
  15. }
  16. preNode = root;
  17. getMin(root.right);
  18. }
  19. }

题解报告LeetCode#1382:将二叉搜索树变平衡

题意:LeetCode#1382:将二叉搜索树变平衡

思路:

  • 中序遍历(递归实现就可以了)
  • 然后根据一个有序数组构造二叉搜索树

代码:

  1. class Solution {
  2. public TreeNode balanceBST(TreeNode root) {
  3. List<Integer> nodes = new LinkedList<>();
  4. inOrder(nodes, root);
  5. int len = nodes.size();
  6. return buildBalanceBST(nodes, 0, len - 1);
  7. }
  8. private TreeNode buildBalanceBST(List<Integer> list, int start, int end) {
  9. if (start > end) {
  10. return null;
  11. }
  12. if (start == end) {
  13. return new TreeNode(list.get(start));
  14. }
  15. int mid = start + (start - end) / 2;
  16. TreeNode root = new TreeNode(list.get(mid));
  17. root.left = buildBalanceBST(list, start, mid - 1);
  18. root.right = buildBalanceBST(list, mid + 1, end);
  19. return root;
  20. }
  21. private void inOrder(List<Integer> list, TreeNode root) {
  22. // 递归一般还是采用这种直接将终止条件写出来的写法,比较能过脑
  23. if (root == null) {
  24. return;
  25. }
  26. inOrder(list, root.left);
  27. list.add(root.val);
  28. inOrder(list, root.right);
  29. }
  30. }

三、后序遍历相关

题解报告LeetCode#104:二叉树的最大深度

题意:LeetCode#104:二叉树的最大深度

思路:后序遍历

代码:

  1. class Solution {
  2. public int maxDepth(TreeNode root) {
  3. return root == null ? 0 : Math.max(maxDepth(root.right), maxDepth(root.left)) + 1;
  4. }
  5. }

题解报告LeetCode#111:二叉树的最小深度

题意:LeetCode#111:二叉树的最小深度

思路:后序遍历,但是要注意,但左节点不为空,右节点为空,不能直接拿左节点来当作最小深度来 + 1,同理右节点。

代码:

  1. class Solution {
  2. public int minDepth(TreeNode root) {
  3. return 0;
  4. }
  5. if (root.left == null) {
  6. return minDepth(root.right) + 1;
  7. }
  8. if (root.right == null) {
  9. return minDepth(root.left) + 1;
  10. }
  11. return Math.min(minDepth(root.left), minDepth(root.right)) + 1;
  12. }

题解报告LeetCode#236:二叉树的最近公共祖先

题意:LeetCode#236:二叉树的最近公共祖先

思路:

1589045080777-305a34a9-0a54-407e-896d-7ebce92735bb.png

  • 由于“最近公共祖先”的定义,它是一个向上传的过程,先知道了左右子树遍历的结果,然后自己才能操作,这是“后序遍历”;
  • 如果 p 和 q 都非空,返回它们的公共祖先;
  • 如果只存在一个;
  • 如果 p 和 q 都不存在,则返回空。

代码:

  1. public class Solution {
  2. public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
  3. if (root == null || root == p || root == q) {
  4. return root;
  5. }
  6. TreeNode left = lowestCommonAncestor(root.left, p, q);
  7. TreeNode right = lowestCommonAncestor(root.right, p, q);
  8. // 下面两行代码不可能同时为空,因为题目保证 p 和 q 一定存在
  9. if (left == null) return right;
  10. if (right == null) return left;
  11. if (left != null && right != null) {
  12. return root;
  13. }
  14. return null;
  15. }
  16. }

三、层序遍历相关