`

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

给定一颗二叉树,输出它的right view,所谓right view就是从右边观察这棵树所能看到的节点。很容易想到的是用广搜,记录每一层最后一个节点。另外我们还可以用深度优先搜索,从树的右子树开始遍历,不过要维护一个变量layer,来判定当前节点是否应该能看到,因为如果右子树为空后,我们要观察左子树,因为左子树的深度可能比右子树大,我们在观察左子树时,通过layer与已经记录的右子树的节点比较,如果layer大于已经记录的节点数,那么这个节点就可以被观察到,如果小于或等于就不能被观察到。下面给出两种方法的代码:
1,广度优先搜索
/**
 * 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 ArrayList<Integer>();
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        if(root == null) return list;
        queue.offer(root);
        int count = 1;
        int helper = 0;
        while(!queue.isEmpty()) {
            TreeNode node = queue.poll();
            count --;
            if(node.left != null) {
                queue.offer(node.left);
                helper ++;
            }
            if(node.right != null) {
                queue.offer(node.right);
                helper ++;
            }
            if(count == 0) {
                list.add(node.val);  
                count = helper;
                helper = 0;
            }
        }
        return list;
    }
}


2,深度优先搜索
/**
 * 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 ArrayList<Integer>();
        if(root == null) return list;
        getView(root, list, 1);
        return list;
    }
    public void getView(TreeNode root, List<Integer> list, int layer) {
        if(root == null) return;
        if(layer > list.size()) list.add(root.val);
        getView(root.right, list, layer + 1);
        getView(root.left, list, layer + 1);
    }
}
0
2
分享到:
评论

相关推荐

    leetcode-tree

    102-Binary Tree Level Order Traversal199-Binary Tree Right Side View:层次遍历的一个运用树的构造给出前中后序的序列中的两个,构造一棵树。递归。前序 parent left-child right-child中序 left-child parent ...

    leetcode:leetcode解题

    leetcode做过的LeetCode的题目记录一下。对一些比较经典的题型进行分类总结。... Binary Tree Right Side View【2020-01-15】130. Surrounded Regions【2020-01-09】114. Flatten Binary Tree to Linked List

    手稿_V1.084

    在LeetCode中,这个问题要求我们实现一个名为`rightSideView`的函数,其目的是从给定的二叉树的右侧视角,按从上到下的顺序返回可见节点的值。给定的标签"C++ 网络 leetcode"表明这是一个与C++编程、数据结构...

    leetcode中文版-LeetCodeAnimation:力码动画

    leetcode中文版 我会尽力将LeetCode上所有的题目都用动画的形式演示出来,计划用3到4年时间去完成它,期待与你见证这一天! 文章最新首发于微信公众号 ...Right Side View 203 移除链表元素 206 反转

    leetcode中文版-cc-leetcode:leetcode/剑指offer算法题个人题解

    "binary-tree-right-side-view"; String answerPath = "leetcode.mid.BinaryTreeRightSideView"; 「剑指Offer」 通过手动输入题目信息生成数据信息,类似LeetCode自动更新Readme.md中的题解表格 使用方式 在 Sword2...

    BobBuilder_app

    Theoretically a b+tree is O(N log k N) or log base k of N, now for the typical values of k which are above 200 for example the b+tree should outperform any binary tree because it will use less ...

    VB编程资源大全(英文源码 控件)

    on left and right side of a text box.&lt;END&gt;&lt;br&gt;40,Assist.zip A simple application with source code which shows how to save the contents of a rich text box without the help of common dialog box.&lt;END&gt;...

    python3.6.5参考手册 chm

    Updated module: ElementTree 1.3 Build and C API Changes Capsules Port-Specific Changes: Windows Port-Specific Changes: Mac OS X Port-Specific Changes: FreeBSD Other Changes and Fixes Porting to ...

Global site tag (gtag.js) - Google Analytics