Leetcode-199(Java) Binary Tree Right Side View

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

传送门:https://leetcode.com/problems/binary-tree-right-side-view/

用优先队列层次遍历的方法,每次遍历到一层的最后一个节点并把它保留即可。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public List<Integer> rightSideView(TreeNode root) {
        List<Integer> list = new LinkedList<Integer>();
        if(root == null)
            return list;
        //保存每一层的节点,用于取出最后一个节点
        Queue<TreeNode> currentLevel = new LinkedList<TreeNode>();
        currentLevel.add(root);
        while(!currentLevel.isEmpty())
        {
            int size = currentLevel.size();
            //lastVal用于保存每层最后一个节点值
            for(int i = 0; i < size - 1; i++)
            {
                TreeNode currentNode = currentLevel.poll();
                if(currentNode.left != null)
                    currentLevel.add(currentNode.left);
                if(currentNode.right != null)
                    currentLevel.add(currentNode.right);
            }
            TreeNode lastNode = currentLevel.poll();
            //把最后一个节点加到队列中
            list.add(lastNode.val);
            if(lastNode.left != null)
                currentLevel.add(lastNode.left);
            if(lastNode.right != null)
                currentLevel.add(lastNode.right);
        }
        return list;
    }
}
时间: 2024-08-03 16:03:25

Leetcode-199(Java) Binary Tree Right Side View的相关文章

Leetcode 随笔之 ------ Binary Tree Right Side View

先等价转一下题意为 “从上至下保存树的每一层的最右节点” 那么我的大致思路可以归纳为: 1. DFS遍历树,把节点和对应的层数标记在一个HashMap里 2. BFS对整棵树构建一个完整的队列, 这个队列的特点就是按一层一层连接起来的 3. 从头遍历队列,根据Hashmap里的层数标记找出每层的最右节点,具体方法是维护一个pre变量保存之前的节点,如果当前节点值与之不等,则把pre加入结果集,另外队尾也要加入结果集 Ps: 本来第二步也打算用iterator遍历结果出了异常,发现是在使用iter

LeetCode OJ:Binary Tree Right Side View(右侧视角下的二叉树)

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

LeetCode OJ:Binary Tree Right Side View

题目: 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,

LeetCode 199. 二叉树的右视图(Binary Tree Right Side View)

199. 二叉树的右视图 199. Binary Tree Right Side View 题目描述 给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. 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. LeetCode19

【LeetCode】199. Binary Tree Right Side View

Binary Tree Right Side View 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 <--- Y

【LeetCode】103. Binary Tree Zigzag Level Order Traversal 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51524241 Subject 出处:https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ri

LeetCode解题报告:Binary Tree Postorder Traversal

Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 注意:下面是迭代的解法.理解有点困难,和大家讨论一下. 1 import java.uti

LeetCode OJ - Balanced Binary Tree

判断树是否是平衡的,这道题中的平衡的概念是指任意节点的两个子树的高度相差不超过1,我用递归的方法把所有的节点的高度都计算了下,并且在计算的过程记录每个节点左右两颗子树的高度差,最后通过遍历这个高度差就可以知道是否是平衡的. 下面是AC代码: 1 /** 2 * Given a binary tree, determine if it is height-balanced. 3 * For this problem, a height-balanced binary tree is defined

LeetCode OJ - construct Binary Tree from Inorder and Postorder/Preorder Traversal

不断递归的实现!!!! 下面是AC代码: 1 /** 2 * Given inorder and postorder traversal of a tree, construct the binary tree. 3 * @param inorder 4 * @param postorder 5 * @return 6 */ 7 public TreeNode buildTree(int[] inorder,int[] postorder){ 8 if(inorder == null || po