zoukankan      html  css  js  c++  java
  • 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;
        }
    }
  • 相关阅读:
    转换进制,十六进制数相加
    一个人的旅行(Dijkstra算法)
    畅通工程续(Dijkstra算法)
    免费书下载
    http://d3js.org/
    React.js model
    jtable更新数据
    java knowledge record
    ActionListener三种实现
    java接口理解(转载)
  • 原文地址:https://www.cnblogs.com/prmlab/p/7278490.html
Copyright © 2011-2022 走看看