• 199. Binary Tree Right Side View


    Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.

    For example:
    Given the following binary tree,

       1            <---
     /   
    2     3         <---
          
      5     4       <---
    

    You should return [1, 3, 4].

    题目含义:从右面观察一颗二叉树,输出由上到下打印的节点

     1     public List<Integer> rightSideView(TreeNode root) {
     2         List<Integer> result = new ArrayList<>();
     3         if (root == null) return result;
     4         Queue<TreeNode> q = new LinkedList<>();
     5         q.add(root);
     6         while (!q.isEmpty())
     7         {
     8             int size = q.size();
     9             TreeNode node = null;
    10             for (int i=0;i<size;i++)
    11             {
    12                 node = q.poll();
    13                 if (node.left !=null) q.offer(node.left);
    14                 if (node.right !=null) q.offer(node.right);
    15             }
    16             result.add(node.val);
    17         }
    18         return result;        
    19     }
  • 相关阅读:
    12.Scala- 注解
    11.Scala-特质
    10.Scala-继承
    9.Scala- 包和引入
    8.Scala-对象
    7.Scala-类
    6.Scala-高阶函数
    5.Scala-匹配模式
    4.Scala-数据结构
    Ruby on Rails Tutorial 第四章 Rails背后的Ruby 之 类
  • 原文地址:https://www.cnblogs.com/wzj4858/p/7714000.html
Copyright © 2020-2023  润新知