zoukankan      html  css  js  c++  java
  • 求二叉树第k层的结点个数

    tag: 二叉树 - 层次遍历

     思路: 用层次遍历思路求解

     辅助: 队列

    package com.zhaochao.tree;
    
    import java.util.ArrayList;
    import java.util.LinkedList;
    import java.util.Queue;
    
    /**
     * Created by zhaochao on 17/1/23.
     */
    public class NodesNumberKLevel {
    
        public int getNodesNumberKLevel(TreeNode root, int k) {
    
            int count = 0;
            if(root == null || k <= 0 || k > getDepthOfTree(root)) {
                return count;
            }
            if(k == 1) {
                return 1;
            }
            Queue<TreeNode> queue = new LinkedList<TreeNode>();
            queue.add(root);
            while(!queue.isEmpty()) {
                int size = queue.size();
                if(count == k - 1) {
                    return size;
                }
                ArrayList<Integer> level = new ArrayList<Integer>();
                for(int i = 0; i < size; i++) {
                    TreeNode head = queue.poll();
                    level.add(head.val);
                    if(head.left != null) {
                        queue.offer(head.left);
                    }
                    if(head.right != null) {
                        queue.offer(head.right);
                    }
                }
                count++;
            }
            return count;
        }
    
        //递归
        public int getK(TreeNode root, int k) {
            if(root == null || k <= 0) {
                return 0;
            }
            if(k == 1) {
                return 1;
            }
            return getK(root.left, k-1) + getK(root.right, k - 1);
        }
    
        public int getDepthOfTree(TreeNode root) {
            if(root == null) {
                return 0;
            }
            int left = getDepthOfTree(root.left);
            int right = getDepthOfTree(root.right);
            return Math.max(left,right) + 1;
        }
    
        public static void main(String[] args) {
            TreeNode root = new TreeNode(0);
            TreeNode node1 = new TreeNode(1);
            TreeNode node2 = new TreeNode(2);
            TreeNode node3 = new TreeNode(3);
    
            root.left = node1;
            root.right = node2;
            node2.left = node3;
    
            NodesNumberKLevel test = new NodesNumberKLevel();
            int numbers = 0;
            numbers = test.getNodesNumberKLevel(root, 1);
            System.out.println("The numbers of nodes of level 3 is : " + numbers);
    
            System.out.println("The numbers of nodes of level 3 is : " + test.getK(root,2));
        }
    }
    

      

      

  • 相关阅读:
    计算机网络中的多路复用技术
    ActiveMQ之一--ActiveMQ入门
    ehcache介绍
    I/O模型之二:Linux IO模式及 select、poll、epoll详解
    【甘道夫】HBase(0.96以上版本号)过滤器Filter具体解释及实例代码
    Android Studio安装及主题字体配置
    HDU 2136 Largest prime factor 參考代码
    update更新两个字段
    Hadoop对小文件的解决方式
    赵雅智_ContentProvider
  • 原文地址:https://www.cnblogs.com/superzhaochao/p/6346824.html
Copyright © 2011-2022 走看看