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

先等价转一下题意为

“从上至下保存树的每一层的最右节点”

那么我的大致思路可以归纳为:

1. DFS遍历树,把节点和对应的层数标记在一个HashMap里

2. BFS对整棵树构建一个完整的队列, 这个队列的特点就是按一层一层连接起来的

3. 从头遍历队列,根据Hashmap里的层数标记找出每层的最右节点,具体方法是维护一个pre变量保存之前的节点,如果当前节点值与之不等,则把pre加入结果集,另外队尾也要加入结果集

Ps: 本来第二步也打算用iterator遍历结果出了异常,发现是在使用iterator访问元素时,不允许其他线程remove或add元素,故新建了一个容器接收弹出的队尾

另外Binary Tree Zigzag Level Order Traversal 这题主要也是划分树层,所以可以用相同的思路来操作

Java Code:

public class Solution {
    Map<TreeNode, Integer> levelMap;
    public List<Integer> rightSideView(TreeNode root) {
        if(root == null)
        return new ArrayList<Integer>();

        List<Integer> list = new ArrayList<Integer>();
        levelMap = new HashMap<TreeNode, Integer>();
        LinkedList<TreeNode> queue = new LinkedList<TreeNode>();

        preorder(root, 1);

        queue.addFirst(root);

        LinkedList<TreeNode> newqueue = new LinkedList<TreeNode>();
        while(!queue.isEmpty()) {
           TreeNode node = queue.removeLast();

           if(node.left != null)
            queue.addFirst(node.left);

           if(node.right != null)
            queue.addFirst(node.right);

            newqueue.addFirst(node);
        }

         TreeNode pre = null;
        while(!newqueue.isEmpty()) {
            TreeNode node = newqueue.removeLast();

            if(pre != null)
            if(levelMap.get(pre) != levelMap.get(node))
            list.add(pre.val);

            if(!newqueue.isEmpty()) {
                pre = node;
            } else {
                list.add(node.val);
            }
        }

        return list;
    }

    public void preorder(TreeNode root, int level) {
        if(root == null)
        return;

        levelMap.put(root,level);
        preorder(root.left, level + 1);
        preorder(root.right, level + 1);

    }

}
时间: 2024-11-07 07:30:58

Leetcode 随笔之 ------ Binary Tree Right Side View的相关文章

【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

&lt;LeetCode OJ&gt; 199. Binary Tree Right Side View

Total Accepted: 40438 Total Submissions: 117654 Difficulty: Medium 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

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

leetcode题解:Construct Binary Tree from Preorder and Inorder Traversal (根据前序和中序遍历构造二叉树)

题目: Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 说明: 1)二叉树可空 2)思路:a.根据前序遍历的特点, 知前序序列(PreSequence)的首个元素(PreSequence[0])为二叉树的根(root),  然后在中序序列(InSequence)中查找此根(

leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal &amp; Construct Binary Tree f

1.  Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 代码: class Solution { public: TreeNode *buildTr