• 199. Binary Tree Right Side View


    https://leetcode.com/problems/binary-tree-right-side-view/description/

    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].

    Sol 1:

    recursion.

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    
    /*
    The core idea of this algorithm:
    
    1.Each depth of the tree only select one node.
    
    View depth is current size of result list.
    
    */
    
    // recursion
    
    
    public class Solution {
        public List<Integer> rightSideView(TreeNode root) {
            List<Integer> result = new ArrayList<Integer>();
            rightView(root, result, 0);
            return result;
        }
        
        public void rightView(TreeNode curr, List<Integer> result, int currDepth){
            if(curr == null){
                return;
            }
            if (currDepth == result.size()){
                result.add(curr.val);
            }
            
            rightView(curr.right, result, currDepth + 1);
            rightView(curr.left, result, currDepth + 1);
        }
    }

    Sol 2:

    iteration

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    
    
    
    // Time O(n) Space O(n)
    
    
    public class Solution {
        public List<Integer> rightSideView(TreeNode root) {
            List<Integer> result = new ArrayList<>();
            if (root == null) {
                return result;
            }
            Queue<TreeNode> queue = new LinkedList<>();
            queue.add(root);
    
            while (!queue.isEmpty()) {
                int size = queue.size();
                for (int i = 0; i < size; i++) {
                    TreeNode node = queue.poll();
                    if (i == size - 1) {
                        // last element in current level
                        result.add(node.val);
                    }
                    if (node.left != null) {
                        queue.add(node.left);
                    }
                    if (node.right != null) {
                        queue.add(node.right);
                    }
                }
            }
            return result;
        }
    }
  • 相关阅读:
    不同进程间消息互发
    不同进程间消息互发
    div滤镜结合ajax,实现登录
    网页自适应不同浏览器和分辨率[转]
    DIV样式汇总
    用CSS中的Alpha实现渐变
    JavaScript中的null和undefined
    CSS教程:div垂直居中的N种方法[转]
    浏览器不兼容原因及解决办法
    JavaScript验证时间格式
  • 原文地址:https://www.cnblogs.com/prmlab/p/7278490.html
Copyright © 2020-2023  润新知