遍历二叉树

  1. public static class TreeNode{
  2. static List qianxu = new ArrayList<>();
  3. static List zhongxu = new ArrayList<>();
  4. static List houxu = new ArrayList<>();
  5. public static void main(String[] args) {
  6. /**
  7. * 0
  8. * 1 2
  9. * 3 4 8 9
  10. * 5 6 10 7
  11. */
  12. Tree tree = new Tree(0);
  13. Tree a = new Tree(1);
  14. Tree b = new Tree(2);
  15. Tree c = new Tree(3);
  16. Tree d = new Tree(4);
  17. Tree e = new Tree(5);
  18. Tree f = new Tree(6);
  19. Tree g = new Tree(7);
  20. Tree h = new Tree(8);
  21. Tree i = new Tree(9);
  22. Tree j = new Tree(10);
  23. tree.left = a;
  24. tree.right = b;
  25. a.left = c;
  26. a.right = d;
  27. c.left = e;
  28. c.right = f;
  29. d.right = g;
  30. b.left = h;
  31. b.right = i;
  32. d.left = j;
  33. List list = houxu(tree);
  34. for(int q =0;q<list.size();q++){
  35. System.out.print(list.get(q)+" ");
  36. }
  37. //cengci(tree);
  38. }
  39. /**
  40. * 前序遍历 中 左 右
  41. * @param node
  42. * @return
  43. */
  44. public static List qianxu(Tree node){
  45. qianxu.add(node.val);
  46. if(node.left != null){
  47. qianxu(node.left);
  48. }
  49. if(node.right != null){
  50. qianxu(node.right);
  51. }
  52. return qianxu;
  53. }
  54. /**
  55. * 中序遍历 左 中 右
  56. * @param node
  57. * @return
  58. */
  59. public static List zhongxu(Tree node){
  60. if(node.left != null){
  61. zhongxu(node.left);
  62. }
  63. zhongxu.add(node.val);
  64. if(node.right != null){
  65. zhongxu(node.right);
  66. }
  67. return zhongxu;
  68. }
  69. /**
  70. * 后序遍历 左 右 中
  71. * @param node
  72. * @return
  73. */
  74. public static List houxu(Tree node){
  75. if(node.left != null){
  76. houxu(node.left);
  77. }
  78. if(node.right != null){
  79. houxu(node.right);
  80. }
  81. houxu.add(node.val);
  82. return houxu;
  83. }
  84. }

层级遍历

  1. /**
  2. * 层次遍历
  3. * @param node
  4. */
  5. public static void cengci(Tree node){
  6. ArrayDeque<Tree> queue = new ArrayDeque<>(20);
  7. //首先将根节点加入栈中
  8. queue.add(node);
  9. //遍历二叉树
  10. while (!queue.isEmpty()) {
  11. //pop()函数和poll()函数都是从首位置取元素并删除;
  12. //区别在于,pop遇到null会报异常。poll遇到null会返回null。
  13. Tree tempNode = queue.poll();
  14. System.out.print(tempNode.val + " ");
  15. if(tempNode.left != null){
  16. queue.add(tempNode.left);
  17. }
  18. if(tempNode.right != null){
  19. queue.add(tempNode.right);
  20. }
  21. }
  22. }

二叉搜索树的最近公共祖先

  1. // 235. 二叉搜索树的最近公共祖先
  2. public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
  3. if(p.val == root.val){
  4. return p;
  5. }
  6. if(q.val == root.val){
  7. return q;
  8. }
  9. if(p.val > root.val && q.val > root.val){
  10. return lowestCommonAncestor(root.right,p,q);
  11. }else if(p.val < root.val && q.val < root.val){
  12. return lowestCommonAncestor(root.left,p,q);
  13. }else{
  14. return root;
  15. }
  16. }

数组转二叉树

思路:涉及二叉树时,一般使用递归

//108. 将有序数组转换为二叉搜索树
    public TreeNode sortedArrayToBST(int[] nums) {
        return buildTree(nums,0,nums.length-1);
    }

    private TreeNode buildTree(int[] nums,int l,int r){
        if(l > r){
            return null;
        }
        int n = l + (r-l)/2;
        TreeNode root = new TreeNode(nums[n]);
        root.left = buildTree(nums,l,n-1);
        root.right = buildTree(nums,n+1,r);
        return root;
    }