二叉树的遍历:
    力扣226,101,222,110
    1.二叉树的反转226

    1. 递归法:
    2. if(root==null){
    3. return root;
    4. }
    5. reverseChildren(root);
    6. invertTree(root.left);
    7. invertTree(root.right);
    8. return root;
    9. }
    10. private void reverseChildren(TreeNode root){
    11. TreeNode temp= root.right;
    12. root.right=root.left;
    13. root.left=temp;
    14. }
    15. 迭代法
    16. if(root==null){
    17. return true;
    18. }
    19. Deque<TreeNode>queue = new LinkedList<>();
    20. queue.addFirst(root.left);
    21. queue.addLast(root.right);
    22. while (!queue.isEmpty()){
    23. TreeNode node1 = queue.pollFirst();
    24. TreeNode node2 = queue.pollLast();
    25. if(node1==null&&node2==null){
    26. continue;
    27. }
    28. if(node1==null||node2==null||node1.val!=node2.val){
    29. return false;
    30. }
    31. queue.addFirst(node1.right);
    32. queue.addLast(node2.left);
    33. queue.addFirst(node1.left);
    34. queue.addLast(node2.right);
    35. }
    36. return true;

    2.对称二叉树101

    1. 递归法
    2. if(root==null){
    3. return true;
    4. }
    5. return mirrorTree(root.left,root.right);
    6. }
    7. private boolean mirrorTree(TreeNode left,TreeNode right){
    8. if(left!=null&&right==null){
    9. return false;
    10. }else if(left==null&&right!=null){
    11. return false;
    12. }else if(left==null&&right==null){
    13. return true;
    14. }
    15. else if(left.val!=right.val){
    16. return false;
    17. }
    18. boolean outside = mirrorTree(left.left, right.right);
    19. boolean inside = mirrorTree(left.right, right.left);
    20. return outside&&inside;

    3.二叉树的节点个数

    1. if(root==null){
    2. return 0;
    3. }
    4. int leftCount = countNodes(root.left);
    5. int rightCount = countNodes(root.right);
    6. return 1+leftCount+rightCount;


    4.平衡二叉树

    1. if(root==null){
    2. return true;
    3. }
    4. getMaxDepth(root);
    5. return res;
    6. }
    7. private int getMaxDepth(TreeNode root){
    8. if(root==null){
    9. return 0;
    10. }
    11. int leftDepth = getMaxDepth(root.left);
    12. int rightDepth = getMaxDepth(root.right);
    13. if(Math.abs(leftDepth-rightDepth)>=2){
    14. res=false;
    15. }
    16. return 1+Math.max(leftDepth,rightDepth);