给定一个二叉树,打印它的右下视图。二叉树的右下视图是从右下侧访问树时可见的一组节点,返回从右到左排序的节点的值。
在右下角视图中,从右下角以 45 度角查看树时,只有少数节点可见,其余节点将隐藏在它们后面。
例子:
Input :
1
/ \
2 3
\ \
5 4
Output : [4, 5]
Visible nodes from the bottom right direction are 4 and 5
Input :
1
/ \
2 3
/ /
4 5
\
6
Output: [3, 6, 4]
方法 :
- 这个问题可以使用简单的递归遍历来解决。
- 通过向所有递归调用传递参数来跟踪节点的级别。以 45% 的角度考虑一棵树的水平(如示例中所述),因此每当我们向左移动时,它的水平都会增加 1。
- 这个想法是跟踪最大级别并以在左子树之前访问右子树的方式遍历树。
- 到目前为止级别超过最大级别的节点,打印该节点,因为这是其级别中的最后一个节点(在左子树之前遍历右子树)。
下面是上述方法的实现:
C++
// C++ program to print bottom
// right view of binary tree
#include
using namespace std;
// A binary tree node
struct Node
{
int data;
Node *left, *right;
Node(int item)
{
data = item;
left = right = NULL;
}
};
// class to access maximum level
// by reference
struct _Max_level
{
int _max_level;
};
Node *root;
_Max_level *_max = new _Max_level();
// Recursive function to print bottom
// right view of a binary tree
void bottomRightViewUtil(Node* node, int level,
_Max_level *_max_level)
{
// Base Case
if (node == NULL)
return;
// Recur for right subtree first
bottomRightViewUtil(node->right,
level, _max_level);
// If this is the last Node of its level
if (_max_level->_max_level < level)
{
cout << node->data << " ";
_max_level->_max_level = level;
}
// Recur for left subtree
// with increased level
bottomRightViewUtil(node->left,
level + 1, _max_level);
}
// A wrapper over bottomRightViewUtil()
void bottomRightView(Node *node)
{
bottomRightViewUtil(node, 1, _max);
}
void bottomRightView()
{
bottomRightView(root);
}
// Driver Code
int main()
{
root = new Node(1);
root->left = new Node(2);
root->right = new Node(3);
root->left->left = new Node(4);
root->right->left = new Node(5);
root->right->left->right = new Node(6);
bottomRightView();
}
// This code is contributed by Arnab Kundu
Java
// Java program to print bottom
// right view of binary tree
// A binary tree node
class Node {
int data;
Node left, right;
Node(int item)
{
data = item;
left = right = null;
}
}
// class to access maximum level by reference
class Max_level {
int max_level;
}
class BinaryTree {
Node root;
Max_level max = new Max_level();
// Recursive function to print bottom
// right view of a binary tree.
void bottomRightViewUtil(Node node, int level,
Max_level max_level)
{
// Base Case
if (node == null)
return;
// Recur for right subtree first
bottomRightViewUtil(node.right,
level, max_level);
// If this is the last Node of its level
if (max_level.max_level < level) {
System.out.print(node.data + " ");
max_level.max_level = level;
}
// Recur for left subtree with increased level
bottomRightViewUtil(node.left,
level + 1, max_level);
}
void bottomRightView()
{
bottomRightView(root);
}
// A wrapper over bottomRightViewUtil()
void bottomRightView(Node node)
{
bottomRightViewUtil(node, 1, max);
}
// Driver program to test the above functions
public static void main(String args[])
{
BinaryTree tree = new BinaryTree();
tree.root = new Node(1);
tree.root.left = new Node(2);
tree.root.right = new Node(3);
tree.root.left.left = new Node(4);
tree.root.right.left = new Node(5);
tree.root.right.left.right = new Node(6);
tree.bottomRightView();
}
}
Python3
# Python3 program to print bottom
# right view of binary tree
# A binary tree node
class Node:
# Construct to create a newNode
def __init__(self, item):
self.data = item
self.left = None
self.right = None
maxlevel = [0]
# Recursive function to print bottom
# right view of a binary tree.
def bottomRightViewUtil(node, level, maxlevel):
# Base Case
if (node == None):
return
# Recur for right subtree first
bottomRightViewUtil(node.right, level,
maxlevel)
# If this is the last Node of its level
if (maxlevel[0] < level):
print(node.data, end = " ")
maxlevel[0] = level
# Recur for left subtree with increased level
bottomRightViewUtil(node.left, level + 1,
maxlevel)
# A wrapper over bottomRightViewUtil()
def bottomRightView(node):
bottomRightViewUtil(node, 1, maxlevel)
# Driver Code
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.right.left = Node(5)
root.right.left.right = Node(6)
bottomRightView(root)
# This code is contributed by SHUBHAMSINGH10
C#
// C# program to print bottom
// right view of binary tree
using System;
// A binary tree node
class Node
{
public int data;
public Node left, right;
public Node(int item)
{
data = item;
left = right = null;
}
}
// class to access maximum level by reference
class Max_level
{
public int max_level;
}
class GFG
{
Node root;
Max_level max = new Max_level();
// Recursive function to print bottom
// right view of a binary tree.
void bottomRightViewUtil(Node node, int level,
Max_level max_level)
{
// Base Case
if (node == null)
return;
// Recur for right subtree first
bottomRightViewUtil(node.right,
level, max_level);
// If this is the last Node of its level
if (max_level.max_level < level)
{
Console.Write(node.data + " ");
max_level.max_level = level;
}
// Recur for left subtree with increased level
bottomRightViewUtil(node.left,
level + 1, max_level);
}
void bottomRightView()
{
bottomRightView(root);
}
// A wrapper over bottomRightViewUtil()
void bottomRightView(Node node)
{
bottomRightViewUtil(node, 1, max);
}
// Driver Code
public static void Main(String []args)
{
GFG tree = new GFG();
tree.root = new Node(1);
tree.root.left = new Node(2);
tree.root.right = new Node(3);
tree.root.left.left = new Node(4);
tree.root.right.left = new Node(5);
tree.root.right.left.right = new Node(6);
tree.bottomRightView();
}
}
// This code is contributed by Princi Singh
Javascript
输出:
3 6 4
时间复杂度: O(N),其中 N 是二叉树的节点数。
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。