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;
    }
}
原文地址:https://www.cnblogs.com/reboot329/p/6107528.html