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;
        }
    }
  • 相关阅读:
    教务管理系统维护日志
    在centOS服务器上部署Discuz!
    iphone5s support.apple com/iphone/restore怎么办
    常见Java面试题
    Mac版免破解office 2011真情放送/Mac版navicat prenium 面破解
    VMware Workstation 12序列号: 5A02H-AU243-TZJ49-GTC7K-3C61N
    centos7搭建docker环境
    加密算法简介
    Hash算法简介
    empty()和size() == 0有区别吗
  • 原文地址:https://www.cnblogs.com/prmlab/p/7278490.html
Copyright © 2011-2022 走看看