第二学期第七周周报

一、本周计划

1、java数据结构的学习

2、线索二叉树

二、完成情况

1、数据结构仍然在二叉树的进度,做了一定的练习

练习1 二叉树先序遍历递归的执行过程

练习2 主要是学了二叉树的遍历并基于此上做的一些练习。

public class Test {
    static class Node{
        public char val;
        public Node left;
        public Node right;
        public Node(char val) {
            this.val = val;
        }

        @Override
        public String toString() {
            return "Node{" +
                    "val=" + val +
                    ", left=" + left +
                    ", right=" + right +
                    '}';
        }
    }
    static Node build(){
        //构建一棵树,返回数的根节点。
        Node A = new Node('A');
        Node B = new Node('B');
        Node C = new Node('C');
        Node D = new Node('D');
        Node E = new Node('E');
        Node F = new Node('F');
        Node G = new Node('G');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        E.left = G;
        C.right = F;
        return A;
    }
    public static int size(Node root) {
        if (root == null) {
            return 0;
        }
        //借助递归思想把问题拆分
        //整个树的节点个数 = 根节点个数+左子树节点个数+右子树节点个数
        return 1 + size(root.left) +size(root.right);
    }

求叶子节点个数

    public  static int leafSize(Node root) {
        if (root == null) {
            return 0;
        }
        if (root.left==null&&root.right==null) {
            return 1;
        }
        //求当前这个树叶子节点的个数 = 左子树中叶子节点的个数 + 右子树中叶子节点个数
        return leafSize(root.left) +leafSize(root.right);
    }

求第k层节点的个数

    public static int kLevelSize(Node root,int k) {
        if (root == null || k < 1) {
            return 0;
        }
        if (k == 1){
            //第一层节点个数
            return 1;
        }
        //左子树的k-1层节点分数 + 右子树k-1层节点个数
        return kLevelSize(root.left,k-1)+kLevelSize(root.right,k-1);
    }

查找某个元素

    public static Node find(Node root,char tofind) {
        if (root == null) {
            return null;
        }
        //先看根节点是不是查找的元素,在递归找左子树,左子树没找到,在递归找右子树
        if (root.val == tofind) {
            return root;
        }
        Node result  = find(root.left,tofind);
        if (result!=null) {
            return result;
        }
        return find(root.right,tofind);
    }

2、理解了线索二叉树的结构和怎样遍历

三、下周计划

处理力扣上的题,多练习。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值