//层次遍历二叉树,并输出节点所在的行数public static void main(String[] args) {TwoTree root = new TwoTree(1);root.left = new TwoTree(2);root.left.left = new TwoTree(3);root.left.left.left = new TwoTree(4);root.right = new TwoTree(5);PRint(root);}public static void print(TwoTree root){Queue<TwoTree> queue = new LinkedList<TwoTree>();TwoTree last = root;TwoTree nlast = root;int num = 1;TwoTree point = root;queue.add(point);while(!queue.isEmpty()){point = queue.poll();System.out.println("行数:"+num+"---"+point.value);if(point.left!=null){queue.add(point.left);nlast = point.left;}if(point.right!=null){queue.add(point.right);nlast = point.right;}if(last == point){last = nlast;num++;}}}public class TwoTree {TwoTree left;TwoTree right;int value;public TwoTree(int a){value = a;left = null;right = null;}}
新闻热点
疑难解答