zoukankan      html  css  js  c++  java
  • 199. Binary Tree Right Side View

    import java.util.ArrayList;
    import java.util.LinkedList;
    import java.util.List;
    import java.util.LinkedList;
    import java.util.Queue;
    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public List<Integer> rightSideView(TreeNode root) {
            List<Integer> res=new ArrayList<Integer>();
            if(root==null)
                return res;
    
            
            
            Queue<TreeNode> current=new LinkedList<TreeNode>();
            
            current.offer(root);
            
            int last=0;
            
            Queue<TreeNode> next=new LinkedList<TreeNode>();
            
            while(!current.isEmpty()||!next.isEmpty())
            {
                if(!current.isEmpty())
                {
                    
                    TreeNode temp=current.poll();
                    last=temp.val;
                    if(temp.left!=null)
                        next.offer(temp.left);
                    if(temp.right!=null)
                        next.offer(temp.right);
                        
                }
                else
                {
                    res.add(last);
                    while(!next.isEmpty())
                        current.offer(next.poll());
                    
                }
            }
            
            res.add(last);
            return res;
            
        }
    }
  • 相关阅读:
    植物园偶遇一直喵
    植物园偶遇一直喵
    美食篇
    美食篇
    端午节路过南站
    端午节路过南站
    黄山云海
    黄山云海
    Android (1)
    树和树算法(1)
  • 原文地址:https://www.cnblogs.com/aguai1992/p/5657440.html
Copyright © 2011-2022 走看看