通用树的左右视图
给定一个由N个节点组成的泛型树,任务是找到给定泛型树的左视图和右视图。
例子:
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。
Input:
1
/ \
2 3
/ \ / \ \
4 5 6 7 8
Output:
Left View: 1 2 4
Right View: 1 3 8
Explanation:
When the tree is viewed from the left side, then the nodes which are visible is 1 2 4.
When the tree is viewed from the right side, then the nodes which are visible is 1 3 8.
Input:
1
/ \
2 3
\ / \
4 5 6
\
7
Output:
Left View: 1 2 4 7
Right View: 1 3 6 7
方法:给定的问题可以通过使用树的层序遍历的概念来解决。在这种情况下,树的遍历是以层级排序的方式完成的。存储层序遍历中的所有节点后,很明显,树的左视图是所有级别的第一个节点,同样,树的右视图是从根开始的所有级别的最后一个节点。
因此,在存储了层序遍历之后,首先为Left View打印存储在每个 level 中的所有第一个节点,并为Right View打印存储在每个 level 中的所有最后一个节点。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Structure of Node
struct Node {
int val;
vector child;
};
// Utility function to create a
// new tree node
Node* newNode(int key)
{
Node* temp = new Node;
temp->val = key;
return temp;
}
// Function to get the left view and
// right view of the given tree
void getLeftRightview(Node* root)
{
// Stores the nodes of each level
queue curNodes;
// Push the root to initiate the
// level ordered traversal
curNodes.push(root);
// Stores the left and right views
vector leftView, rightView;
// Iterate until queue is non-empty
while (!curNodes.empty()) {
// Find the number of ndoes in
// current level
int n = curNodes.size();
for (int i = 0; i < n; i++) {
Node* cur = curNodes.front();
curNodes.pop();
// If the node is first node
// in the level
if (i == 0)
leftView.push_back(
cur->val);
// If the node is last node
// in the level
if (i == n - 1)
rightView.push_back(
cur->val);
// Push all the childs of the
// current node into the queue
for (int it = 0;
it < cur->child.size(); it++) {
curNodes.push(
cur->child[it]);
}
}
}
// Print the left view
cout << "Left View: ";
for (int i = 0; i < leftView.size(); i++)
cout << leftView[i] << ' ';
cout << endl;
// Print the right view
cout << "RIght View: ";
for (int i = 0; i < rightView.size(); i++)
cout << rightView[i] << ' ';
}
// Driver Code
int main()
{
Node* root = newNode(1);
(root->child).push_back(newNode(2));
(root->child).push_back(newNode(3));
(root->child[0]->child).push_back(newNode(4));
(root->child[1]->child).push_back(newNode(6));
(root->child[0]->child).push_back(newNode(5));
(root->child[1])->child.push_back(newNode(7));
(root->child[1]->child).push_back(newNode(8));
getLeftRightview(root);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
public class Main
{
// Structure of Node
static class Node {
public int val;
public Vector child;
public Node(int key)
{
val = key;
child = new Vector();
}
}
// Utility function to create a
// new tree node
static Node newNode(int key)
{
Node temp = new Node(key);
return temp;
}
// Function to get the left view and
// right view of the given tree
static void getLeftRightview(Node root)
{
// Stores the nodes of each level
Vector curNodes = new Vector();
// Push the root to initiate the
// level ordered traversal
curNodes.add(root);
// Stores the left and right views
Vector leftView = new Vector();
Vector rightView = new Vector();
// Iterate until queue is non-empty
while (curNodes.size() > 0) {
// Find the number of ndoes in
// current level
int n = curNodes.size();
for (int i = 0; i < n; i++) {
Node cur = (Node)curNodes.get(0);
curNodes.remove(0);
// If the node is first node
// in the level
if (i == 0)
leftView.add(cur.val);
// If the node is last node
// in the level
if (i == n - 1)
rightView.add(cur.val);
// Push all the childs of the
// current node into the queue
for (int it = 0; it < cur.child.size(); it++) {
curNodes.add(cur.child.get(it));
}
}
}
// Print the left view
System.out.print("Left View: ");
for (int i = 0; i < leftView.size(); i++)
System.out.print(leftView.get(i) + " ");
System.out.println();
// Print the right view
System.out.print("RIght View: ");
for (int i = 0; i < rightView.size(); i++)
System.out.print(rightView.get(i) + " ");
}
public static void main(String[] args) {
Node root = newNode(1);
(root.child).add(newNode(2));
(root.child).add(newNode(3));
(root.child.get(0).child).add(newNode(4));
(root.child.get(1).child).add(newNode(6));
(root.child.get(0).child).add(newNode(5));
(root.child.get(1)).child.add(newNode(7));
(root.child.get(1).child).add(newNode(8));
getLeftRightview(root);
}
}
// This code is contributed by divyeshrabadiya07.
Python3
# Python3 implementation for the above approach
import sys
# Structure of Node
class Node:
def __init__(self, key):
self.val = key
self.child = []
# Utility function to create a
# new tree node
def newNode(key):
temp = Node(key)
return temp
# Function to get the left view and
# right view of the given tree
def getLeftRightview(root):
# Stores the nodes of each level
curNodes = []
# Push the root to initiate the
# level ordered traversal
curNodes.append(root)
# Stores the left and right views
leftView, rightView = [], []
# Iterate until queue is non-empty
while (len(curNodes) != 0):
# Find the number of ndoes in
# current level
n = len(curNodes)
for i in range(n):
cur = curNodes[0]
curNodes.pop(0)
# If the node is first node
# in the level
if (i == 0):
leftView.append(cur.val)
# If the node is last node
# in the level
if (i == n - 1):
rightView.append(cur.val)
# Push all the childs of the
# current node into the queue
for it in range(len(cur.child)):
curNodes.append(cur.child[it])
# Print the left view
print("Left View: ", end = "")
for i in range(len(leftView)):
print(leftView[i], "", end = "")
print()
# Print the right view
print("RIght View: ", end = "")
for i in range(len(rightView)):
print(rightView[i], "", end = "")
root = newNode(1)
(root.child).append(newNode(2))
(root.child).append(newNode(3))
(root.child[0].child).append(newNode(4))
(root.child[1].child).append(newNode(6))
(root.child[0].child).append(newNode(5))
(root.child[1]).child.append(newNode(7))
(root.child[1].child).append(newNode(8))
getLeftRightview(root)
# This code is contributed by rameshtravel07.
C#
// C# program for the above approach
using System;
using System.Collections;
using System.Collections.Generic;
class GFG {
// Structure of Node
class Node {
public int val;
public List child;
public Node(int key)
{
val = key;
child = new List();
}
}
// Utility function to create a
// new tree node
static Node newNode(int key)
{
Node temp = new Node(key);
return temp;
}
// Function to get the left view and
// right view of the given tree
static void getLeftRightview(Node root)
{
// Stores the nodes of each level
Queue curNodes = new Queue();
// Push the root to initiate the
// level ordered traversal
curNodes.Enqueue(root);
// Stores the left and right views
List leftView = new List();
List rightView = new List();
// Iterate until queue is non-empty
while (curNodes.Count > 0) {
// Find the number of ndoes in
// current level
int n = curNodes.Count;
for (int i = 0; i < n; i++) {
Node cur = (Node)curNodes.Peek();
curNodes.Dequeue();
// If the node is first node
// in the level
if (i == 0)
leftView.Add(cur.val);
// If the node is last node
// in the level
if (i == n - 1)
rightView.Add(cur.val);
// Push all the childs of the
// current node into the queue
for (int it = 0; it < cur.child.Count; it++) {
curNodes.Enqueue(cur.child[it]);
}
}
}
// Print the left view
Console.Write("Left View: ");
for (int i = 0; i < leftView.Count; i++)
Console.Write(leftView[i] + " ");
Console.WriteLine();
// Print the right view
Console.Write("RIght View: ");
for (int i = 0; i < rightView.Count; i++)
Console.Write(rightView[i] + " ");
}
static void Main() {
Node root = newNode(1);
(root.child).Add(newNode(2));
(root.child).Add(newNode(3));
(root.child[0].child).Add(newNode(4));
(root.child[1].child).Add(newNode(6));
(root.child[0].child).Add(newNode(5));
(root.child[1]).child.Add(newNode(7));
(root.child[1].child).Add(newNode(8));
getLeftRightview(root);
}
}
// This code is contributed by mukesh07.
Javascript
Left View: 1 2 4
RIght View: 1 3 8
时间复杂度: O(N)
辅助空间: O(N)