• 199. Binary Tree Right Side View


    一个树,从右边看是什么样。

    想到的是BFS,把每层最后一个添加到res里。

    public class Solution {
        public List<Integer> rightSideView(TreeNode root) 
        {
            List<Integer> res = new ArrayList<>();
            
            if(root == null) return res;
            
            Queue<TreeNode> q = new LinkedList<>();
            q.add(root);
            int cur = 0;
            int total = 1;
            while(!q.isEmpty())
            {
                TreeNode temp = q.poll();
                total--;
                
                
                if(temp.left!=null)
                {
                    q.add(temp.left);
                    cur++;
                }
                    
                if(temp.right!=null)
                {
                    q.add(temp.right);
                    cur++;
                }
                if(total == 0)
                {
                    total = cur;
                    cur = 0;
                    res.add(temp.val);
                }
                
            }
            
            return res;
        }
    }
    

    一刷是用DFS做的,也是蛮惊人的。

    有一个全局变量来记录已经添加到第几层了。当前层数如果比全局变量要低,就添加。然后全局 + +

    为了保证从右边,所以遍历的时候先遍历right node再left node

    public class Solution {
        
        int total = 0;
        public List<Integer> rightSideView(TreeNode root) {
            List<Integer> list = new ArrayList<Integer>();
            if(root == null) return list;
            
            
            
            trave(list,root,0);
            
            return list;
        }
        
        public void trave(List<Integer> list, TreeNode root, int height)
        {
            if(root == null) return;
            
            if(height+1 > total)
            {
                list.add(root.val);
                total++;
            }
            trave(list,root.right,height+1);
            trave(list,root.left,height+1);
        }
    }
    

    第二种做法快一些,毕竟不用牵扯Queue的操作吧。。


    三刷。

    这题白送的。

    看自己二刷总结一刷。。明显在他妈胡说八道。。希望以后回过头,不要觉得三刷的自己是智障。

    这个题BFS DFS都行,其实就是level order traversal

    然后BFS每层添加最后一个;DFS从右往左preOrder,每层添加第一个。。

    recursion:

    public class Solution {
        public List<Integer> rightSideView(TreeNode root) {
            List<Integer> res = new ArrayList<>();
            preOrder(root, res, 0);
            return res;
        }
        
        public void preOrder(TreeNode root, List<Integer> res, int level) {
            if (root == null) return;
            
            if (level == res.size()) {
                res.add(root.val);    
            }
            preOrder(root.right, res, level + 1);
            preOrder(root.left, res, level + 1);
        }
    }
    

    iteration:

    public class Solution {
        public List<Integer> rightSideView(TreeNode root) {
            List<Integer> res = new ArrayList<>();
            if (root == null) return res;
            
            Queue<TreeNode> q = new LinkedList<>();
            q.offer(root);
            TreeNode temp = null;
            int left = 1;
            int total = 0;
            
            while (!q.isEmpty()) {
                temp = q.poll();
                
                if (temp.left != null) {
                    q.offer(temp.left);
                    total ++;
                }
                
                if (temp.right != null) {
                    q.offer(temp.right);
                    total ++;
                }
                
                if (--left == 0) {
                    left = total;
                    total = 0;
                    res.add(temp.val);
                }
            }
            
            return res;
        }
    }
    
  • 相关阅读:
    Program C--二分
    Program A-归并排序
    Program E-- CodeForces 18C
    Program B--CodeForces 492B
    2015 HUAS Provincial Select Contest #1 C
    2015 HUAS Provincial Select Contest #1 B
    2015 HUAS Provincial Select Contest #1 A
    CSU 1111.三家人。第三次选拔赛D题:整理花园酬劳分配问题
    将10进制整数转换成16进制整数输出
    -UVa10935题:Trowing cards away1解答及简单分析
  • 原文地址:https://www.cnblogs.com/reboot329/p/6107528.html
Copyright © 2020-2023  润新知