travel the binary tree by level( from top to down)

个人信息:就读于燕大本科软件工程专业 目前大三;

本人博客:google搜索“cqs_2012”即可;

个人爱好:酷爱数据结构和算法,希望将来从事算法工作为人民作出自己的贡献;

博客内容:travel the binary tree by level( from top to down);

博客时间:2014-5-3;

编程语言:C++ ;

编程坏境:Windows 7 专业版 x64;

编程工具:vs2008 32位编译器;

制图工具:office 2010 ppt;

硬件信息:7G-3 笔记本;

my words

if I rest, I rust.

problem( from <beauty of programming>)

travel the binary tree by level( from top to down )

eg: the following binary tree

the travelling result follows

my solution

solve this problem by queue, visit every node while it  be popped and push its children while it has.

void _TravelByLevel(node * T)
{
	queue<node *> Q;
	cout<<"travel start"<<endl;
	if(T != NULL)
	{
		Q.push(T);
		node * p;
		while(! Q.empty())
		{
			// first action
			p = Q.front();
			Q.pop();
			_Visit(p);
			// second action
			if(p->left != NULL)
				Q.push(p->left);
			if(p->right != NULL)
				Q.push(p->right);
		}
	}
	cout<<"travel over"<<endl;

}

my code

test.cpp

#include<iostream>
#include<queue>
using namespace std;

class node
{
public:
    int data ;
    node * left ;
    node * right ;
    node()
	{
        data = 0 ;
        left = right = NULL ;
    }
};

void _MakeTree(node * &T,int *data,int length);
void _Insert(node * & T,int data);
void _Visit(node * T);
void _TravelByLevel(node * T);

void _MakeTree(node * &T,int *data,int length)
{
    for(int i=0;i<length;i++)
    {
		_Insert(T,data[i]);
    }
}

void _Insert(node * & T,int data)
{
    if(T == NULL)
    {
		T = new node();
		T -> data = data;
    }
    else
    {
		node * p = T;
		while(p != NULL)
		{
			if(data == p->data )
			break;
			else if(data < p->data )
			{
				if(p->left != NULL)
				{
					p = p ->left;
				}
				else{
					p->left = new node();
					(p->left) ->data = data;
					break;
				}
			}
			else{
				if(p->right != NULL)
					p = p->right;
				else{
					p->right = new node() ;
					(p->right) ->data = data ;
					break ;
				}
			}
		}
    }
}

void _TravelByLevel(node * T)
{
	queue<node *> Q;
	cout<<"travel start"<<endl;
	if(T != NULL)
	{
		Q.push(T);
		node * p;
		while(! Q.empty())
		{
			// first action
			p = Q.front();
			Q.pop();
			_Visit(p);
			// second action
			if(p->left != NULL)
				Q.push(p->left);
			if(p->right != NULL)
				Q.push(p->right);
		}
	}
	cout<<"travel over"<<endl;

}

void _Visit(node * T)
{
	cout<<T->data<<endl;
}

int main()
{
    int data[] = {8,6,3,7,2,1,13,12,15,17};

    node * T = NULL;
    _MakeTree(T,data,10);
	_TravelByLevel(T);
    system("pause");
    return 0;
}

travel the binary tree by level( from top to down)

时间: 2024-11-23 00:43:46

travel the binary tree by level( from top to down)的相关文章

travel the binary tree by level 2 ( from top to down )

个人信息:就读于燕大本科软件工程专业 目前大三; 本人博客:google搜索"cqs_2012"即可; 个人爱好:酷爱数据结构和算法,希望将来从事算法工作为人民作出自己的贡献; 博客内容:travel the binary tree by level 2 ( from top to down ); 博客时间:2014-5-3; 编程语言:C++ ; 编程坏境:Windows 7 专业版 x64; 编程工具:vs2008 32位编译器; 制图工具:office 2010 ppt; 硬件信

travel the binary tree by level 4 ( from down to top and from left to right every level )

个人信息:就读于燕大本科软件工程专业 目前大三; 本人博客:google搜索"cqs_2012"即可; 个人爱好:酷爱数据结构和算法,希望将来从事算法工作为人民作出自己的贡献; 博客内容:travel the binary tree by level 4 ( from down to top and from left to right every level ) 博客时间:2014-5-3; 编程语言:C++ ; 编程坏境:Windows 7 专业版 x64; 编程工具:vs2008

travel the binary tree by level 3 ( from down to top and from right to left every level )

travel the binary tree by level 3 ( from down to top ) 个人信息:就读于燕大本科软件工程专业 目前大三; 本人博客:google搜索"cqs_2012"即可; 个人爱好:酷爱数据结构和算法,希望将来从事算法工作为人民作出自己的贡献; 博客内容:travel the binary tree by level 3 ( from down to top ) 博客时间:2014-5-3; 编程语言:C++ ; 编程坏境:Windows 7

travel the binary tree by level 5 ( from down to top and from left to right every level )

个人信息:就读于燕大本科软件工程专业 目前大三; 本人博客:google搜索"cqs_2012"即可; 个人爱好:酷爱数据结构和算法,希望将来从事算法工作为人民作出自己的贡献; 博客内容:travel the binary tree by level 5 ( from down to top and from left to right every level ) 博客时间:2014-5-4; 编程语言:C++ ; 编程坏境:Windows 7 专业版 x64; 编程工具:vs2008

leetcode 刷题之路 63 Binary Tree Zigzag Level Order Traversal

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its zig

leetcode -day24 Maximum Depth of Binary Tree &amp; Binary Tree Zigzag Level Order Traversal

1.Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. class Solution { public: int maxDepth(TreeNode *root) { inM

leetcode_103题——Binary Tree Zigzag Level Order Traversal(广度优先搜索,队列queue,栈stack)

Binary Tree Zigzag Level Order Traversal Total Accepted: 31183 Total Submissions: 117840My Submissions Question Solution Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left fo

37. Binary Tree Zigzag Level Order Traversal &amp;&amp; Binary Tree Inorder Traversal

Binary Tree Zigzag Level Order Traversal Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tree {3,9,20,

Binary Tree Zigzag Level Order Traversal

原题: 题目解析:这个问题的实质是要我们按成访问二叉树的结点,并返回每层访问的结果,这里要求走Z字,其实就是一行正向一行反向. /* the kernel idea is visit a binary search tree in level and the additional work we have to label the end of one level. */ vector<vector<int> > zigzagLevelOrder(TreeNode *root) {