Binary tree number of nodes with a given level

|▌冷眼眸甩不掉的悲伤 提交于 2021-02-11 12:42:57

问题


I need to write a program that counts the number of nodes from a certain level given in binary tree.

I mean < numberofnodes(int level){} >

I tried writing it without any success because I don't how to get to a certain level then go to count number of nodes.


回答1:


Do it with a recursive function which only descends to a certain level.




回答2:


Well, there are many ways you can do this. Best is to have a single dimensional array that keep track of the number of nodes that you add/remove at each level. Considering your requirement that would be the simplest way.

However, if provided with just a binary tree, you WILL have to traverse and go to that many levels and count the nodes, I do not see any other alternative.

To go to a certain level, you will typically need to have a variable called as 'current_depth' which will be tracking the level you are in. Once you reach your level of interest and that the nodes are visited once (typically a In order traversal would suffice) you can increment your count. Hope this helped.




回答3:


I'm assuming that your binary tree is not necessarily complete (i.e., not every node has two or zero children or this becomes trivial). I'm also assuming that you are supposed to count just the nodes at a certain level, not at that level or deeper.

There are many ways to do what you're asked, but you can think of this as a graph search problem - you are given a starting node (the root of the tree), a way to traverse edges (the child links), and a criteria - a certain distance from the root.

At this point you probably learned graph search algorithms - which algorithm sounds like a natural fit for your task?




回答4:


In general terms:
Recursion.
In each iteration of the recursion, you need to measure somehow what level are you on, and therefore to know how far down the tree you need to go beyond where you are now.
Recursion parts:

  1. What are you base cases? at what conditions do you say "Okay, time to stop the recursion" ?
  2. How can you count something in a recursion without any global count?



回答5:


I think the easiest is to simply follow the recursive nature of the tree with an accumulator to track the current level (or maybe the number of levels remaining to be descended).

The non-recursive branch of that function is when you hit the level in question. At that point you simply return 1 (because you found one node at that level).

The recursive branch, simply sums the number of nodes at that level returned from the left and right recursive invocations.




回答6:


This is the pseudo-code, this assumes the root has level 0

int count(x,currentLevel,desiredLevel)
    if currentLevel = desiredLevel
        return 1
    left = 0
    right = 0
    if x.left != null
        left = count(x.left, currentLevel+1, desiredLevel
    if x.right != null
        right = count(x.right, currentLevel+1, desiredLevel
    return left + right

So to get the number of nodes for level 3 you call

count(root,0,3)


来源:https://stackoverflow.com/questions/2853189/binary-tree-number-of-nodes-with-a-given-level

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!