• 算法刷题——二叉树部分操作(翻转二叉树,平衡二叉树,最大深度)


     翻转二叉树



    1. package 二叉树.反转二叉树;
    2. import 二叉树.TreeNode;
    3. import java.util.ArrayList;
    4. import java.util.LinkedList;
    5. import java.util.List;
    6. import java.util.Queue;
    7. public class Solution {
    8. public static void main(String[] args) {
    9. TreeNode root=new TreeNode(1);
    10. TreeNode node2=new TreeNode(2);
    11. TreeNode node3=new TreeNode(3);
    12. TreeNode node4=new TreeNode(4);
    13. TreeNode node5=new TreeNode(5);
    14. TreeNode node6=new TreeNode(6);
    15. TreeNode node7=new TreeNode(7);
    16. TreeNode node8=new TreeNode(8);
    17. root.setLeft(node2);
    18. root.setRight(node3);
    19. node2.setLeft(node4);
    20. node2.setRight(node5);
    21. node3.setRight(node6);
    22. node5.setLeft(node7);
    23. node5.setRight(node8);
    24. System.out.println(root);
    25. List integers=new ArrayList<>();
    26. inorderTraversal(root,integers);
    27. System.out.println(integers);
    28. List newRootIntegers=new ArrayList<>();
    29. TreeNode newRoot=invertTree(root);
    30. inorderTraversal(newRoot,newRootIntegers);
    31. System.out.println(newRootIntegers);
    32. }
    33. public static TreeNode invertTree(TreeNode root) {
    34. TreeNode node=null;
    35. if (root==null){
    36. return null;
    37. }
    38. invertTree(root.left);
    39. invertTree(root.right);
    40. node=root.right;
    41. root.right=root.left;
    42. root.left=node;
    43. return root;
    44. }
    45. public static void inorderTraversal(TreeNode root,List integers ){
    46. if (root==null){
    47. return;
    48. }
    49. inorderTraversal(root.left,integers);
    50. integers.add(root.val);
    51. inorderTraversal(root.right,integers);
    52. }
    53. }

     

     二叉树最大深度

     力扣

    1. package 二叉树.最大深度;
    2. import 二叉树.TreeNode;
    3. import java.util.ArrayList;
    4. import java.util.LinkedList;
    5. import java.util.List;
    6. import java.util.Queue;
    7. public class Solution {
    8. public static void main(String[] args) {
    9. TreeNode root=new TreeNode(1);
    10. TreeNode node2=new TreeNode(2);
    11. TreeNode node3=new TreeNode(3);
    12. TreeNode node4=new TreeNode(4);
    13. TreeNode node5=new TreeNode(5);
    14. TreeNode node6=new TreeNode(6);
    15. TreeNode node7=new TreeNode(7);
    16. TreeNode node8=new TreeNode(8);
    17. root.setLeft(node2);
    18. root.setRight(node3);
    19. node2.setLeft(node4);
    20. node2.setRight(node5);
    21. node3.setRight(node6);
    22. node5.setLeft(node7);
    23. node5.setRight(node8);
    24. // int max = maxDepth(root);
    25. int max = maxDepthMethodForLoop(root);
    26. System.out.println("----------二叉树最大深度为------"+max);
    27. }
    28. public static int maxDepth(TreeNode root) {
    29. int deep=0;
    30. if(root==null){
    31. return 0;
    32. }else{
    33. return Math.max( maxDepth(root.right), maxDepth(root.left))+1;
    34. }
    35. }
    36. public static int maxDepthMethodForLoop(TreeNode root) {
    37. int deep=0;
    38. Queue queue=new LinkedList<>();
    39. queue.offer(root);
    40. while (!queue.isEmpty()){
    41. int size=queue.size();
    42. while (size>0){
    43. TreeNode treeNode=queue.poll();
    44. if(treeNode.left!=null){
    45. queue.offer(treeNode.left);
    46. }
    47. if(treeNode.right!=null){
    48. queue.offer(treeNode.right);
    49. }
    50. System.out.println(queue);
    51. System.out.println(queue.size());
    52. System.out.println(size);
    53. size--;
    54. }
    55. deep++;
    56. }
    57. return deep;
    58. }
    59. }

    平衡二叉树

    力扣​​​​​​​

    1. package 二叉树.平衡二叉树;
    2. import 二叉树.TreeNode;
    3. import java.util.ArrayList;
    4. import java.util.List;
    5. public class Solution {
    6. public static void main(String[] args) {
    7. TreeNode root=new TreeNode(1);
    8. TreeNode node2=new TreeNode(2);
    9. TreeNode node3=new TreeNode(3);
    10. TreeNode node4=new TreeNode(4);
    11. TreeNode node5=new TreeNode(5);
    12. TreeNode node6=new TreeNode(6);
    13. TreeNode node7=new TreeNode(7);
    14. TreeNode node8=new TreeNode(8);
    15. root.setLeft(node2);
    16. root.setRight(node3);
    17. node2.setLeft(node4);
    18. node2.setRight(node5);
    19. node3.setRight(node6);
    20. node5.setLeft(node7);
    21. node5.setRight(node8);
    22. int balanced = isBalanced(root);
    23. System.out.println(balanced);
    24. }
    25. public static int isBalanced(TreeNode root) {
    26. if(root==null){
    27. return 0;
    28. }
    29. int left=isBalanced(root.left);
    30. int right=isBalanced(root.right);
    31. System.out.println(left);
    32. System.out.println(right);
    33. if(left==-1||right==-1||Math.abs(left-right)>1){
    34. return -1;
    35. }
    36. return Math.max(left,right)+1;
    37. }
    38. }

  • 相关阅读:
    为什么避免在循环、条件或嵌套函数中调用 Hooks
    SpringBoot——MVC自动配置原理《课时十三》
    PMAL: Open Set Recognition via Robust Prototype Mining
    Java核心编程(13)
    Python 全栈系列191 基于Redis队列的处理服务
    1、年千亿营收企业Java研发人员日常使用Linux命令总结
    基于神经网络的图像分割,图像识别神经网络算法
    十二、stm32-红外遥控(OLED显示)
    【vue实战项目】通用管理系统:登录页
    mysql解压版安装步骤linux
  • 原文地址:https://blog.csdn.net/qq_33839972/article/details/128136756