当前位置:   article > 正文

二叉树练习题

二叉树练习

1. 获取树中节点的个数

int count = 0;
int size(TreeNode root){
   
    if (root == null){
   
        return 0;
    }
    count++;
    return size(root.left)+size(root.right);
}
/**
 * 子问题思路
 */
int size1(TreeNode root){
   
    if (root == null) return 0;
    //左子树和右子树之和+根节点
    return size1(root.left)+size1(root.right)+1;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

2. 获取叶子节点的个数 - (遍历思路、子问题思路)

  • 遍历思路:遍历到叶子结点,就让计数器++
int leafCount = 0;
void getLeafNodeCount(TreeNode root){
   
    if(root == null){
   
        return;
    }
    //左子树和右子树都为null时,是叶子结点
    if (root.left == null && root.right ==null){
   
        leafCount++;
    }
    //递归左子树和右子树
    getLeafNodeCount(root.left);
    getLeafNodeCount(root.right);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 子问题思路 :左树叶子数 +右树叶子数 = 整棵树的叶子
int leafCount1 = 0;
int getLeafNodeCount1(TreeNode root) {
   
    if (root == null) {
   
        return 0;
    }
    if (root.left == null &&root.right == null){
   
        //当前的root没有子树,自己就是叶子节点
        return 1;
    }
    return getLeafNodeCount1(root.left) + getLeafNodeCount1(root.right);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

3. 获取第K层节点的个数

  • 子问题思路:是根节点的第k层,也就是第二层结点的第k-1层
public int getKLevelNodeCount(TreeNode root,int k){
   
    if (root == null || k <= 0){
   
        return 0;
    }
    if (k == 1){
    //当k=1时,第一层
        return 1;
    }
    //k不为1时:递归
    return getKLevelNodeCount(root.left,k-1) + getKLevelNodeCount(root.right,k-1);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

4. 获取二叉树的高度

  • 子问题思路:左树的高度和右树的高度 取最大值 然后+1(根节点)= 整棵树的高度
  • 下面的函数时间复杂度:O(n) (看递归的次数,每个结点都递归,次数为n)
  • 空间复杂度:O(k) k=log2(n) (空间复杂度为数的高度)
int getHeight2(TreeNode root) {
   
    if(root == null){
   
        return 0;
    }
    int leftHeight = getHeight2(root.left);
    int rightHeight = getHeight2(root.right);
    return leftHeight>=rightHeight ? leftHeight+1 : rightHeight+1;


}
int getHeight(TreeNode root){
   
    if(root == null){
   
        return 0;
    }
    //比较左树高度和右树高度,取最大值
    int leftHeight = getHeight(root.left);
    int rightHeight = getHeight(root.right);
    return leftHeight > rightHeight ? leftHeight+1 : rightHeight+1;

   //return getHeight(root.left) > getHeight(root.right)? (getHeight(root.left)+1):(getHeight(root.right)+1);
   //递归的次数太多了,重复计算了好多次,err
        //超出规定的运行时间:1.死循环 2.递归的次数太多了
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

5. 检测值为value的元素是否存在

TreeNode find(TreeNode root,char val) {
   
    if (root == null) return null;
    if (root.val == val){
   
        return root;
    }
    TreeNode ret = find(root.left,val);
    if(ret!=null){
   
        return ret;
    }
    ret = find(root.right,val); //如果左子树没有值为value的元素,看看右子树
    if (ret != null){
   
        return ret;
    }
    return null;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

6. 判断一棵树是不是完全二叉树

  • 思路:root为空返回true,不为空时,建立一个队列,将root放入队列,
  • 队列不为空时,每次取出队列队头元素,对头元素不为null,则加入对头元素的左子树和右子树,为null则中止循环
  • 之后,队列不为空时,检测队列中所有元素,如果都是null则为true,如果有别的元素,则为false
{
   
    if(root == null){
   
        return true;
    }
    //队列
    Queue<TreeNode> queue = new LinkedList<>();
    queue.offer(root);//将根入队列
    //循环
    while (!queue.isEmpty()){
   // 队列不为空
        TreeNode cur = queue.poll(); //取出队头元素给cur
        if (cur != null){
     //当取出的队头不为null时,加入其左子树和右子树两个元素
            queue.offer(cur.left);
            queue.offer(cur.right);
        }else {
   
            break; //为null时,中止循环
        }
    }
    //看队列剩下的元素是否都为null,如果有不为null的元素,那么就不是完全二叉树
    while(!queue.isEmpty()){
   
        TreeNode top = queue.peek();
        if(top != null){
   
            return false;
        }
        queue.poll();
    }
    return true;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

7. 相同的树

时间复杂度:O(min(M,N))

  • https://leetcode.cn/problems/same-tree/
public boolean isSameTree(TreeNode p, TreeNode q) {
   
    if(p == null && q == null){
    //
  • 1
  • 2
  • 3
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/290211
推荐阅读
相关标签
  

闽ICP备14008679号