二叉树右视图_视图算法的类型二叉树

二叉树右视图

Binary Tree is a special data structure used for data storage purposes. A binary tree has a special condition that each node can have a maximum of two children.

二进制树是一种特殊的数据结构,用于数据存储。 二叉树有一个特殊条件,即每个节点最多可以有两个孩子。

Every node (excluding a root) in a tree is connected by a directed edge from exactly one other node. This node is called a parent. On the other hand, each node can be connected to an arbitrary number of nodes, called children.

树中的每个节点(不包括根)都通过有向边与正好一个其他节点相连。 该节点称为父节点。 另一方面,每个节点可以连接到任意数量的称为子节点的节点。

In this article, we will look into various view algorithms of the binary tree and we will discuss each and every algorithm with a common pattern with some twist and twigs of some line of code. 🙌

在本文中,我们将研究二叉树的各种视图算法,并且将讨论具有相同模式,带有一些代码行的扭曲和细枝的每种算法。 🙌

We will first understand one algorithm intuitive by which we will create a template and solve all other problems.

我们将首先了解一种直观的算法,通过它我们将创建模板并解决所有其他问题。

Types of View in Binary Tree are :

二叉树中的视图类型为:

  1. Horizontal View

    水平视图

  2. Vertical View

    垂直视图

  3. Left View

    左视图

  4. Right View

    右视图

  5. Top View

    顶视图

  6. Bottom View

    底视图

Horizontal View: Let first understand the Horizontal View of Binary Tree or Level Order View.

^ h orizo​​ntal查看:咱们先了解二叉树或水平的采购观的水平视图。

Image for post

A Tree is represented by at most two children left and right. First of all, we know that can be traversed a tree by BFS(Queue) or DFS(Stack) in the problem statement it is mentioned about Level Order so What can we use BFS or DFS?

一棵树最多由左右两个孩子表示。 首先,我们知道BFS (队列)或DFS (堆栈)可以在提到有关级别顺序的问题陈述中遍历树,那么我们可以使用BFS或DFS?

BFS right!! So we can use the Queue data structure for storing the node value as simple as that. So the root will be at level 0 and below that root right and root left will be at level 1 and so forth and so on….

BFS吧! 因此,我们可以使用Queue数据结构来存储节点值,就这么简单。 因此,根将位于级别0并在该根以下,根的左下方将处于级别1,依此类推,依此类推……。

We need to store Node and Level in a queue so we can use a Pair Data structure to store value in pairs. First, we will store the root node value and level as 0 into the queue. We will iterate until the queue is not empty and we will store the left and right child node and (level +1) into the queue each time. We will add the node value into the ArrayList at a certain level.

我们需要将Node和Level存储在队列中,以便我们可以使用Pair Data结构成对存储值。 首先,我们将根节点值和级别作为0存储到队列中。 我们将迭代直到队列不为空,并且每次将左右子节点和(级别+1)存储到队列中。 我们将在特定级别将节点值添加到ArrayList中。

public List<Integer> HorizontalView(TreeNode root) 
{
        List<List<Integer>> horizontalView = new ArrayList<>();
        Queue<Pair<TreeNode,Integer>> queue= new LinkedList<>();
        int level=0;
        queue.add(new Pair(root,level));
        while(!queue.isEmpty())
        {
            Pair<TreeNode,Integer> n=queue.poll();
            TreeNode node=n.getKey();
            int l=n.getValue();
            if(node!=null)
            {
                if(horizontalView.size()<l+1)
                {
                    horizontalView.add(new ArrayList<>());
                }
                horizontalView.get(l).add(node.val);
                queue.add(new Pair(node.left,l+1));
                queue.add(new Pair(node.right,l+1));
            }
            
        }
        return horizontalView;  
}

So as you have looked in the above code to better understand the concept we have created this as a template for all other problems.

因此,正如您在上面的代码中浏览的内容一样,为了更好地理解该概念,我们将其创建为所有其他问题的模板。

The Overall Time Complexity is O(N )and Space Complexity is O(N)

时间复杂度O(N)空间复杂度O(N)

Image for post

Right View: If you can see the tree from the right side all the nodes that's are ending node of each list. As we have understand how to solve a problem for the Horizontal view we will use the template and add a few lines of code to solve this problem.

[R 洞察力;查看:如果你能看到从右侧的所有节点这是结束每个列表的节点的树。 当我们了解如何解决“水平”视图的问题时,我们将使用模板并添加几行代码来解决此问题。

List<Integer> rightView= new ArrayList<>();
        for(int i=0;i<list.size();i++)
        {
            rightView.add(list.get(i).get(list.get(i).size()-1));
        }

In the above code, we need to iterate through the ArrayList of Horizontal View and get the last value from each List and add to the new ArrayList.

在上面的代码中,我们需要遍历“水平视图”的ArrayList并从每个List中获取最后一个值,然后添加到新的ArrayList中。

Image for post
List<Integer> leftView= new ArrayList<>();
        for(int i=0;i<list.size();i++)
        {
            leftView.add(list.get(i).get(0);
        }

Left View: If you can see the tree from the left side all the nodes are starting node of each list. In the above code, we need to iterate through the ArrayList of Horizontal View and get the first value from each List and add to the new ArrayList.

视图:如果您可以从左侧看到树,则所有节点都是每个列表的起始节点。 在上面的代码中,我们需要遍历Horizo​​ntal View的ArrayList并从每个List中获取第一个值,然后添加到新的ArrayList中。

Image for post

Bottom View: If you can see the tree from the bottom for a Full Binary Tree the last list from the Horizontal View is the bottom view.

ottom查看:如果你可以从底部看树 对于完整的二叉树,水平视图的最后一个列表是底部视图。

public List<Integer> rightSideView(TreeNode root) 
 {
        List<List<Integer>> rightView = new ArrayList<>();
        Queue<Pair<TreeNode,Integer>> queue= new LinkedList<>();
        int level=0;
        queue.add(new Pair(root,level));
        while(!queue.isEmpty())
        {
            Pair<TreeNode,Integer> n=queue.poll();
            TreeNode node=n.getKey();
            int l=n.getValue();
            if(node!=null)
            {
                if(list.size()<l+1)
                {
                    rightView.add(new ArrayList<>());
                }
                rightView.get(l).add(node.val);
                queue.add(new Pair(node.left,l+1));
                queue.add(new Pair(node.right,l+1));
            }
            
        }
        return rightView.get(size()-1);
        
}
Image for post

Top View: From the above implementation if you can think of looking from the top of the tree you will see every outmost and innermost node at each level. Iterating through the ArrayList of Horizontal View and getting the first and last value of each list except the root value. Where root value is only one element in the tree so we can insert the root.

Ť 运算观:从如果你能想到的从树上,你会看到在每个级别每个最外层和最内层节点的顶部看上面的实现。 遍历“水平视图”的ArrayList并获取除根值以外的每个列表的第一个和最后一个值。 其中根值只是树中的一个元素,因此我们可以插入根。

List<Integer> list= new ArrayList<>();
        for(int i=0;i<list.size();i++)
        {
          if(i==0)
          {
            list.add(list.get(i).get(0));
          }
          else
          {
            list.add(list.get(i).get(0));
            list.add(list.get(i).get(list.get(i).size()-1));
          }
        }
Image for post

Vertical View: Let understand this as it is a little different problem from all the problem that we have done till now. Let recall that in Horizontal View we took root as level 0 and root.right and root.left as level 1 and so forth and so on ….The idea is root will be level 0 but root.right will have a (currentlevel+1) and root.left will be (currentlevel-1) and store this in the list.

V ertical查看:咱们明白这一点,因为它是从所有的问题有一些不同的问题,我们已经做了到现在。 让我们回想一下,在“水平视图”中,我们将root设为0级别,将root.right和root.left设为1级别,依此类推,依此类推……等等。想法是root将是0级别,而root.right将具有( currentlevel + 1 )和root.left将是( currentlevel-1)并将其存储在列表中。

Think wisely which data structure we can use to solve this problem ??

明智地思考我们可以使用哪种数据结构来解决这个问题?

The Map data structure can be used Right !!!! we can store a key as level and values as ArrayList.

可以使用Map数据结构对!!!! 我们可以将键存储为级别,将值存储为ArrayList。

The Concept is same but little change we have to use the map to store vertical view list.

概念是相同的,但是变化很小,我们必须使用地图存储垂直视图列表。

public List<List<Integer>> VerticalView(TreeNode root) 
 {
         List<List<Integer>> verticalView = new ArrayList<>();
        Queue<Pair<TreeNode,Integer>> queue= new LinkedList<>();
        Map<Integer,List<Integer>> map = new HashMap<>();
        int level=0;
        queue.add(new Pair(root,level));
        while(!queue.isEmpty())
        {
            Pair<TreeNode,Integer> n=queue.poll();
            TreeNode node=n.getKey();
            int l=n.getValue();
            if(node!=null)
            {
                if(map.getOrDefault(l,new ArrayList<>()).size()==0)
                {
                    map.put(l,new ArrayList<>());
                }
                map.get(l).add(node.val);
                queue.add(new Pair(node.left,l-1));
                queue.add(new Pair(node.right,l+1));
            }
            
        }
        
        for (Map.Entry<Integer,List<Integer>> entry : map.entrySet())
        {
            verticalView.add(new ArrayList<>(entry.getValue()));
        }
             
        return verticalView;
        
   }

So I hope you had understood the basic concept of View Algorithms to solve any view related problem of the binary tree.

因此,我希望您了解视图算法的基本概念,以解决二叉树中与视图相关的任何问题。

Please let me know in the comment below what is your opinion about these algorithms.

请在下面的评论中让我知道您对这些算法有何看法。

翻译自: https://medium.com/@surajdey8603/types-of-view-algorithms-binary-tree-e3321fadba6a

二叉树右视图

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值