原创转载请注明出处:http://agilestyle.iteye.com/blog/2360924
method 1 —— 使用两个队列
use a queue to store elements and levels
keep dequeing elements until the associated levels equals to the desired level
print out elements and stop when queue is empty
method 2 —— 递归
recursion
keep track of current level versus desired level
print out element when current = desired
package org.fool.java.test; import java.util.LinkedList; import java.util.Queue; public class TreeLevelPrintTest { public static void main(String[] args) { // 4 // 2 6 // 1 3 5 7 Tree myTree = new Tree(4); myTree.left = new Tree(2); myTree.right = new Tree(6); myTree.left.left = new Tree(1); myTree.left.right = new Tree(3); myTree.right.left = new Tree(5); myTree.right.right = new Tree(7); // use method 1 printTreeLevel(myTree, 2); // 1 3 5 7 System.out.println(); printTreeLevel(myTree, 1); // 2 6 System.out.println(); printTreeLevel(myTree, 0); // 4 System.out.println(); // use method 2 printTreeLevel(myTree, 0, 2); System.out.println(); printTreeLevel(myTree, 0, 1); System.out.println(); printTreeLevel(myTree, 0, 0); } // method 1, use Queue private static void printTreeLevel(Tree t, int desire) { if (desire < 0) { return; } // now define 2 queues, one to store tree nodes, the other for current levels Queue<Tree> trees = new LinkedList<>(); Queue<Integer> levels = new LinkedList<>(); // start by pushing root node in the queue trees.add(t); levels.add(0); // now define a loop to continue while the queue is not empty while (!trees.isEmpty()) { Tree temp = trees.poll(); int currentLevel = levels.poll(); if (temp == null) { continue; } else if (currentLevel == desire) { System.out.print(temp.value + " "); } else { // need to continue to its child tree nodes trees.add(temp.left); levels.add(currentLevel + 1); trees.add(temp.right); levels.add(currentLevel + 1); } } } // method 2, use recursion private static void printTreeLevel(Tree t, int currentLevel, int desire) { if(t == null || currentLevel > desire) { return; } if(currentLevel == desire) { System.out.print(t.value + " "); } else { // proceed to its left and right sub-trees printTreeLevel(t.left, currentLevel + 1, desire); printTreeLevel(t.right, currentLevel + 1, desire); } } private static class Tree { public int value; public Tree left; public Tree right; public Tree(int value) { this.value = value; this.left = null; this.right = null; } } }
Console Output
Reference
https://www.youtube.com/watch?v=79fPL0F_1XA&index=43&list=PLlhDxqlV_-vkak9feCSrnjlrnzzzcopSG
相关推荐
java java_leetcode题解之Find Elements in a Contaminated Binary Tree.java
Construct Binary Tree from Preorder and Inorder Traversal 根据先序,中序建立二叉树
4. `The Binary Tree.doc`:可能是关于二叉树的文档或说明,详细解释了二叉树的概念或者代码库的使用方法。 5. `BinaryTree_Debug.projdata`, `BinaryTree_Debug.projdata1`:这些可能是Visual Studio调试配置相关的...
An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to 6) is traversed, the ...
BinaryTree-BinaryTree
java java_leetcode题解之Closest Leaf in a Binary Tree.java
在"Search in a Binary Search Tree"这个主题中,我们主要探讨如何在二叉搜索树中高效地进行查找操作。查找操作的目标是找到树中与给定值相匹配的节点。由于二叉搜索树的特性,我们可以采用分治策略来实现快速查找:...
This is a binary tree search implementation.
c++ C++_leetcode题解之671. Second Minimum Node In a Binary Tree.cpp
def print_tree(node, level=0): if node is not None: print(' ' * level * 2, node.val) print_tree(node.left, level+1) print_tree(node.right, level+1) # 创建一个二叉树示例 root = TreeNode(1) root....
java基础 java_leetcode题解之All Nodes Distance K in Binary Tree.java
二叉树是一种重要的数据结构,它在计算机科学中扮演着至关重要的角色,特别是在算法和...在二叉树的`BinaryTree`文件中,可能会包含这些操作的具体实现,通过阅读和理解这些代码,可以深入学习和掌握二叉树的相关知识。
二叉树是一种在计算机科学中广泛使用的数据结构,它的每个节点最多有两个子节点,通常称为左子节点和右...解压“binarytree.rar”,查看其中的文件,理解数据结构,并根据给定的描述编写代码,以实现二叉树的前序遍历。
在IT领域,二叉树(Binary Tree)是一种基础的数据结构,尤其在计算机科学中有着广泛的应用。二叉树是每个节点最多有两个子节点的树结构,通常分为左子节点和右子节点。在这个"java-二叉树binaryTree"主题中,我们将...
【标题】:“BinaryTree-源码.rar”是一个与二叉树相关的源代码压缩包,它可能包含各种二叉树数据结构的实现,如二叉搜索树、平衡二叉树(AVL树或红黑树)或者自定义的二叉树结构。这个压缩包可能为学习数据结构与...
BinaryTreeSort的java实现,简单的二叉树排序
"Python-BinaryTree"是一个专门用于学习和操作二叉树的Python库,它提供了方便的API来创建、遍历和操作二叉树。 1. **二叉树的概念与类型** - 二叉树的基本概念:二叉树的每个节点包含一个值、一个指向左子树的...
A Complete Binary Tree (CBT) is a tree that is completely filled, with the possible exception of the bottom level, which is filled from left to right. Now given a sequence of distinct non-negative ...
C++实现 操作函数包括先序、中序、后序遍历,求深度,深度、广度遍历 构建二叉树
在给定的“二叉树官方源码BinaryTree_src”中,我们可以找到一系列与二叉树相关的源代码文件,这为理解和实现二叉树提供了宝贵的参考资料。 首先,我们看到一个名为"BinaryTreeDemo.clw"的文件,这可能是项目的工作...