2021-03-20

1.先序遍历
访问根结点,递归处理左子树,递归处理右子树
2.中序遍历
递归处理左子树,访问根结点,递归处理右子树
3.
4.检查两棵树是否相同
体会递归
两个树相同=根结点相同&&左子树相同&&右子树相同
5.二叉树的最大深度(高度)
=1+ max(左子树高度,右子树高度)
6.判断一颗二叉树是否是平衡二叉树
(这棵树上的任意节点的左右子树的高度差不超过1)
在这里插入图片描述这两个都是平衡二叉树
基本思路:遍历二叉树,计算当前节点左右子树的高度差

public  class   BinaryTree{
    public  int  getHeight(TreeNode   root){
          if(root==null){
               return  O;
          }
          int  leftHeight=getHeight (root.left);
          int  rightHeight=getHeight (root.right);
          return   1+(getHeight(root.left)>getHeight(root.right)?getHeight(root.left):getHeight(root.right));
   }
public  boolean  isBalanced(TreeNode  root){
      //先进行遍历
       if(root==null){
           return  true;
       }
       if(root.left==null&&root.right==null){
           return  true;
       }
       int leftHeight=getHeight(root.left);
       int reightHeight=getHeight(root.reight);
       if((leftHeight-rightHeight)>1‖((leftHeight-rightHeight)<-1{
         return   false;
      }
      return  isBlanced(root.left)&&isBlanced(root.right);
}

7.对称二叉树
在这里插入图片描述

基本思路:所谓的对称和根节点关系不大,主要是看左右子树是否互为"镜像关系"
比较左右子树镜像关系=左右子树根节点的值是否相同&&左子树的左子树和右子树的右子树是否镜像&&左子树的右子树和柚右子树的左子树是否镜像

public   boolean  isSymmetric(TreeNode    root){
    if(root==null){
        return   true;
    }
    return   isMirror(root.left,root.right);
}
public   boolean  isMirror(TreeNode  p,TreeNode  q){
    if(p==null&&q==null){
       return  true;
    }
    if(p==null||q==null){
       return   false;
    }
    if(p.val!=q.val){
       return   false;
    }
   return   isMirror(p.left,q.right)&&isMirror(p.right,q.left);
}

8.二叉树的层序遍历
在这里插入图片描述

A B C D E F G
思路:
(1)取出队首元素
(2)访问(打印)这个元素
(3)把这个打印出的元素的左右子树入队列(非空)
(4)重复执行(|)

public  static  void  LevelOrder(TreeNode  root){
   if(root = null){
       return;
   }
Queue<TreeNode>queue=new  LinkedList<>();
queue.offer(root);
whiletrue{
TreeNode  cur = queue.poll();
if(cur==null){
break;
}
system.out.print(cur.val);
//把该节点的左右子树入队列
if(cur.left!=null){
queue.offer(cur.left);
}
if(cur.right!=null){
queue.offer(cur.right);
}
public  static  TreeNode build(){
TreeNode a =new  TreeNode (val:1;
TreeNode b =new  TreeNode (val:2;
TreeNode c =new  TreeNode (val:3;
TreeNode d =new  TreeNode (val:4;
TreeNode e =new  TreeNode (val:5;
TreeNode f =new  TreeNode (val:6;
TreeNode g =new  TreeNode (val:7;
a.left = b;
a,right = c;
b.left = d;
b.right = e;
e.left = g;
c.right = f;
return a;
}
 public  static  viod  main(string[] args){
    TreeNode  root = build();
    levelOrder(root);
  }
}

选中代码,按ctrl+Alt+t触发surround功能选择需要包裹的代码即可
9.完全二叉树
(1)针对这个二叉树进行层序遍历
(2)遍历过程中针对这个二叉树的判定要分成两个阶段来看待
①第一个阶段,要求每个节点必须有两个子树 ,如果遇到某个节点只有左子树或者没有子树,进入第二阶段
如果遇到某个节点只有右子树,那么这个树不是完全二叉树
②第二阶段,要求每个节点必须没有子树,如果某个节点有子树,则不是完全二叉树

public  static  boolean isCompleteTree(TreeNode  root){
   if(root =null){
      returnve true;
   }
   boolean  isLevel2=false;
   Queue<TreeNode>queue=new  LinkedList<>();
   queue.offer(root);
   whiletrue{
       TreeNode   cur=queue.poll;
       if(cur==null){
       break;
   }
  if(!isLevel2){
     if(cur.left!=null&&cur.right!=null){
         queue.offer(cur.left);
         queue.offer(cur.right);
  }else if(cur.left==null&&cur.right!=null){
         return  false;
  }else if(cur.left!=null&&cur.right==null){
         islevel2=true;
         queue.offer(cur.left);
  }else{
         isLevel2=true;
  }
  }else{
 ifelse if(cur.left!=null || cur.right!=null){
     return false;
 }
return  true;
}
2021-03-26 20:54:33,596 - Model - INFO - Epoch 1 (1/200): 2021-03-26 20:57:40,380 - Model - INFO - Train Instance Accuracy: 0.571037 2021-03-26 20:58:16,623 - Model - INFO - Test Instance Accuracy: 0.718528, Class Accuracy: 0.627357 2021-03-26 20:58:16,623 - Model - INFO - Best Instance Accuracy: 0.718528, Class Accuracy: 0.627357 2021-03-26 20:58:16,623 - Model - INFO - Save model... 2021-03-26 20:58:16,623 - Model - INFO - Saving at log/classification/pointnet2_msg_normals/checkpoints/best_model.pth 2021-03-26 20:58:16,698 - Model - INFO - Epoch 2 (2/200): 2021-03-26 21:01:26,685 - Model - INFO - Train Instance Accuracy: 0.727947 2021-03-26 21:02:03,642 - Model - INFO - Test Instance Accuracy: 0.790858, Class Accuracy: 0.702316 2021-03-26 21:02:03,642 - Model - INFO - Best Instance Accuracy: 0.790858, Class Accuracy: 0.702316 2021-03-26 21:02:03,642 - Model - INFO - Save model... 2021-03-26 21:02:03,643 - Model - INFO - Saving at log/classification/pointnet2_msg_normals/checkpoints/best_model.pth 2021-03-26 21:02:03,746 - Model - INFO - Epoch 3 (3/200): 2021-03-26 21:05:15,349 - Model - INFO - Train Instance Accuracy: 0.781606 2021-03-26 21:05:51,538 - Model - INFO - Test Instance Accuracy: 0.803641, Class Accuracy: 0.738575 2021-03-26 21:05:51,538 - Model - INFO - Best Instance Accuracy: 0.803641, Class Accuracy: 0.738575 2021-03-26 21:05:51,539 - Model - INFO - Save model... 2021-03-26 21:05:51,539 - Model - INFO - Saving at log/classification/pointnet2_msg_normals/checkpoints/best_model.pth 我有类似于这样的一段txt文件,请你帮我写一段代码来可视化这些训练结果
02-06
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值