binary tree breadth first traversal in c++

BFS is faster to find shortest path from root to leaf node of a tree. But the tradeoff is to use more memory.

the basic trategy is to maintain a list to hold nodes of each level.

 1 void BFS(TreeNode<T>* node){
 2    if(node == NULL) // if node is NULL, return
 3       return;
 4   else{
 5      std::list<TreeNode<T>* > current;
 6      current.push_back(node);
 7      while( current.size != 0){
 8         std::list<TreeNode<T>* > next_level;
 9         for(std::list<TreeNode<T>* >::iterator itr = current.beign();
10               itr != current.end(); ++itr){
11            if(node->left) next_level.push_back(node->left);
12            if(node->right) next_level.push_back(node->right);
13         }
14         current = next_level;
15     }
16   }
17 }

you could use iterator to traverse the current list to print out the values as well.

时间: 2025-01-03 15:50:37

binary tree breadth first traversal in c++的相关文章

[LeetCode]Binary Tree Level Order Traversal II

Binary Tree Level Order Traversal II Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 re

Leetcode题解(1):L102/Binary Tree Level Order Traversal

L102: Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 return its level order

33: Binary Tree Level Order Traversal II

/************************************************************************/        /*       33:      Binary Tree Level Order Traversal II                                         */        /**************************************************************

Binary Tree Level Order Traversal II 解题思路

思路: 与Binary Tree Level Order Traversal I 几乎一样.只是最后将结果存放在栈里,然后在栈里再传给向量即可. 再次总结思路: 两个queue,先把第一个放进q1,循环q1是否为空,不为空就读取并出列,如果root有孩子就放入q2,最后清空q2. 注意: for循环的时候不要使用vector.size()这类作为最大值判断,由于vector的size可能不断的减小,这回导致遍历不完的情况发生. 即: int count = sret.size(); for(in

LeetCode OJ - Binary Tree Level Order Traversal 1 &amp;&amp; 2

BFS以及它的扩展,我发现栈是个很好用的数据结构,特别是对于顺序需要颠倒的时候!!! 这里有个重要的信息:可以用null来标识一个level的结束!!! 下面是AC代码: 1 /** 2 * Given a binary tree, return the bottom-up level order traversal of its nodes' values. 3 * (ie, from left to right, level by level from leaf to root). 4 *

leetCode 107. Binary Tree Level Order Traversal II 二叉树层次遍历反转

107. Binary Tree Level Order Traversal II Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example:Given binary tree [3,9,20,null,null,15,7],     3  

32: Binary Tree Level Order Traversal

/************************************************************************/        /*       32:      Binary Tree Level Order Traversal                                          */        /****************************************************************

Binary Tree Level Order Traversal java实现

Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its level order traversal as: [ [3], [9,20], [15,7] ] 实现的关键在

[leetcode]Binary Tree Level Order Traversal @ Python

原题地址:http://oj.leetcode.com/problems/binary-tree-level-order-traversal/ 题意:二叉树的层序遍历的实现. 解题思路:二叉树的层序遍历可以用bfs或者dfs来实现.这里使用的dfs实现,代码比较简洁.实际上,二叉树的先序遍历就是dfs实现.   比如一棵树如下: 1 /  \ 2       3 /    \    /   \ 4     5  6    7    二叉树的先序遍历为{1,2,4,5,3,6,7},可以看到这个遍