[英]Left or Right View of an Tree
什么是提供树的左/右视图的有效代码。
EX: -
1
/ \
left view--->> 4 7 <<--right view
/ \ /
3 2 9
/
8
该树的左视图是-1 4 3 8,右视图是-1 7 9 8
我已经尝试过级别顺序遍历,但是如果树上有一些失踪的孩子,那么我很难找到级别的起点(在左侧视图的情况下)或终点(在右侧视图的情况下),请给建议
仅使用单个队列来获得左(或右)视图并不困难。 将最右边的子节点排入队列后,在队列中插入“ null”作为标记,以在进行层顺序遍历时标记下一个(子)层的结束。
class Node{
Node left, right;
int value;
Node(int value){
left=right=null;
this.value = value;
}
}
public class BinaryTree
{
Node root;
public void leftView(){
//Using single queue.
boolean leftmost = true;
if(this.root == null) return;
Queue<Node> q = new LinkedList<>();
q.add(this.root);
q.add(null);
while(q.isEmpty() == false){
Node rear = q.poll();
if(leftmost == true) {
if(rear == null) break;
System.out.print(rear.value + " ");
leftmost = false;
}
if(rear.left != null) q.add(rear.left);
if(rear.right != null) q.add(rear.right);
if(q.peek() == null) {
leftmost = true;
q.poll(); //remove it from rear
q.add(null); //add it at front.
}
}
//OUTPUT : 12 10 25 50
}
public static void main (String[] args) throws java.lang.Exception
{
BinaryTree bt = new BinaryTree();
bt.root = new Node(12);
bt.root.left = new Node(10);
bt.root.right = new Node(30);
bt.root.right.left = new Node(25);
bt.root.right.left.left = new Node(50);
bt.root.right.right = new Node(40);
// 12
// / \
// 10 30
// / \
// 25 40
// /
// 50
bt.leftView();
}
}
你是对的,通过像这样的水平顺序遍历
Queue<Node> queue = new ArrayDeque<Node>();
for (Node node = root; node != null; node = queue.poll()) {
if (node.leftChild != null) queue.add(node.leftChild);
if (node.rightChild != null) queue.add(node.rightChild);
}
很难知道一个级别在哪里结束而下一个级别在哪里开始。 可以通过使用两个队列来解决。
Queue<Node> currentLevel = new ArrayDeque<Node>();
if (root != null) currentLevel.add(root);
while (true) {
Node node = currentLevel.poll();
if (node == null) break;
/* node is the leftmost on its level */
Queue<Node> nextLevel = new ArrayDeque<Node>();
do {
if (node.leftChild != null) nextLevel.add(node.leftChild);
if (node.rightChild != null) nextLevel.add(node.rightChild);
node = currentLevel.poll();
} while (node != null);
currentLevel = nextLevel;
}
打印左视图的算法:
这是最终代码:
class BinaryTree {
class TreeNode {
int data;
TreeNode left;
TreeNode right;
public TreeNode(int data) {
this.data=data;
}
}
private static int maxLevel = -1;
public static leftView(TreeNode root) {
left_view(root, 0);
}
private static void left_view(BTNode root, int level) {
if (root == null)
return;
if (level > maxLevel) {
System.out.println(root.data);
maxLevel = level;
}
left_view(root.left, level + 1);
left_view(root.right, level + 1);
}
包com.Trees.sumit;
导入java.util.LinkedList; 导入java.util.Queue;
公共类LeftView {
public static void main(String[] args) {
// TODO Auto-generated method stub
TreeNode root = createBinaryTree();
Queue<TreeNode> queue = new LinkedList<>();
System.out.println("Left View" );
queue.add(root);
while (!queue.isEmpty()) {
System.out.println(queue.peek().data);
int queueSize = queue.size();
while (queueSize > 0) {
TreeNode removedNode = queue.poll();
if (removedNode.left != null)
queue.add(removedNode.left);
if (removedNode.right != null)
queue.add(removedNode.right);
queueSize--;
}
}
}
public static class TreeNode {
int data;
TreeNode left;
TreeNode right;
public TreeNode(int data) {
super();
this.data = data;
}
}
private static TreeNode createBinaryTree() {
// TODO Auto-generated method stub
TreeNode rootNode = new TreeNode(40);
TreeNode root20 = new TreeNode(20);
TreeNode root10 = new TreeNode(10);
TreeNode root30 = new TreeNode(30);
TreeNode root50 = new TreeNode(50);
TreeNode root55 = new TreeNode(55);
TreeNode root57 = new TreeNode(57);
TreeNode root60 = new TreeNode(60);
TreeNode root70 = new TreeNode(70);
rootNode.left = root20;
rootNode.right = root60;
root50.right = root55;
root55.right = root57;
root20.left = root10;
root20.right = root30;
root60.left = root50;
root60.right = root70;
return rootNode;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.