統計二叉樹每層節點個數並列印每層節點,統計二叉樹節點個數

來源:互聯網
上載者:User

統計二叉樹每層節點個數並列印每層節點,統計二叉樹節點個數

先看百度一道面試題:

有一顆二叉樹,定義樹的高度為從根到葉子節點的最長距離,樹的寬度為每層節點的最大值,樹的面積定義為高度和寬度的乘積。寫一個Function Compute一個二叉樹的面積。(15分)

解決這道題參考資料劍指offer和編程之美,分別求求二叉樹高度和寬度。


都是採用遞迴:

求高度參考:http://blog.csdn.net/buyingfei8888/article/details/38345591

求每層節點個數需要先求出高度

代碼:

#include <iostream>using namespace std;typedef struct node{int data;struct node *lchild;struct node *rchild;}Node ,*pNode;void createTree(pNode & root){int temp;scanf("%d",&temp);if(0 != temp){root=(pNode) malloc (sizeof(Node));root->data = temp;createTree(root->lchild);createTree(root->rchild);}else{root=NULL;}}void printAll(const pNode root){    pNode p = root;if(p){cout<< p->data<< " ";printAll(p->lchild);printAll(p->rchild);}}//列印一層節點int printNodeAtLevel(pNode root,int level,int &count){if(!root || level <0)return 0;if(0 == level){cout<< root->data<< " ";count++;return 1;}return printNodeAtLevel(root->lchild,level-1,count) + printNodeAtLevel(root->rchild,level-1,count);}//類似後序遍曆,效率比較高,不會重複訪問節點void  btDepth(const pNode  root,int &depth){if(NULL == root){depth=0;return ;}int left ;btDepth(root->lchild,left);    int right ;btDepth(root->rchild,right);depth = left > right ? left +1 : right +1;}int main(){pNode root=NULL;createTree(root);cout<<endl;printAll(root);cout<<endl;int depth=0;btDepth(root,depth);cout<<endl<<depth;cout<<endl;//迴圈遍曆 ,列印各層幾點for(int i=0;i<depth;i++){int count=0;printNodeAtLevel(root,i,count);cout << " 節點個數為:"<< count<<endl;}return 0;}

運行結果:




怎統計一個二叉樹每一層的節點個數

import java.io.*;
import java.util.*;

class TreeNode{//節點類
public String data;//節點資料
public TreeNode lchild,rchild;//左孩子及右孩子
public TreeNode(String d){// 節點建構函式
System.out.println("node created");//構造標誌
data = d;
}
public TreeNode(){// 節點建構函式
System.out.println("node created");
}
public void visitNode(){//輸出節點資料
System.out.print(data);
}
}
public class BiTree{
public BiTree(){//主類建構函式
TreeNode tn = new TreeNode();//建立新樹根為空白
TreeNode tn2 = tn;
tn.visitNode();
System.out.println("start creating tree");//開始建樹標誌
try{//建立二叉樹
createBiTree(tn);
}catch(IOException e){
e.printStackTrace();
}
outputTree(tn);//按層輸出二叉樹
}
String instr;//接受輸入的字串變數
String over = new String(" ");//結束符標誌
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public TreeNode createBiTree(TreeNode tn1)throws IOException{//從鍵盤輸入字串,按先序次序輸入並建立二叉樹
System.out.println("input node");
try{//從鍵盤輸入節點資料
instr = br.readLine();
}catch(IOException e){//捕獲異常並輸出異常資訊
e.printStackTrace();
}
if(instr.equals(over)){//如果為空白則表示沒有節點
System.out.println("null node");
tn1 = null;
}
else{//否則建立節點,並為該節點的左孩及右孩子建立節點如此迴圈
tn1.data = instr;
tn1.lchild = new TreeNode();
tn1.lchild = createBiTree(tn1.lchild);
tn1.rchild......餘下全文>>
 
怎統計一個二叉樹每一層的節點個數

import java.io.*;
import java.util.*;

class TreeNode{//節點類
public String data;//節點資料
public TreeNode lchild,rchild;//左孩子及右孩子
public TreeNode(String d){// 節點建構函式
System.out.println("node created");//構造標誌
data = d;
}
public TreeNode(){// 節點建構函式
System.out.println("node created");
}
public void visitNode(){//輸出節點資料
System.out.print(data);
}
}
public class BiTree{
public BiTree(){//主類建構函式
TreeNode tn = new TreeNode();//建立新樹根為空白
TreeNode tn2 = tn;
tn.visitNode();
System.out.println("start creating tree");//開始建樹標誌
try{//建立二叉樹
createBiTree(tn);
}catch(IOException e){
e.printStackTrace();
}
outputTree(tn);//按層輸出二叉樹

}
String instr;//接受輸入的字串變數
String over = new String(" ");//結束符標誌
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public TreeNode createBiTree(TreeNode tn1)throws IOException{//從鍵盤輸入字串,按先序次序輸入並建立二叉樹
System.out.println("input node");
try{//從鍵盤輸入節點資料
instr = br.readLine();
}catch(IOException e){//捕獲異常並輸出異常資訊
e.printStackTrace();
}
if(instr.equals(over)){//如果為空白則表示沒有節點
System.out.println("null node");
tn1 = null;
}
else{//否則建立節點,並為該節點的左孩及右孩子建立節點如此迴圈
tn1.data = instr;
tn1.lchild = new TreeNode();
tn1.lch......餘下全文>>
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.