leetcode 199. Binary Tree Right Side View-树的右视图|递归|非递归|Java|Python

原创 2016年05月31日 22:20:00
 原题链接:199. Binary Tree Right Side View

【思路】非递归实现

层次遍历,每一层都是从左到右遍历,将每一层的最右一个节点添加到结果集中就是我们需要得到的结果:

public class Solution {
    public List<Integer> rightSideView(TreeNode root) {
        List<Integer> res = new ArrayList<Integer>();
        if (root == null) return res;
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        queue.add(root);
        while (!queue.isEmpty()) {
            int n = queue.size();
            while(n-- > 0) {
                root = queue.poll();
                if(root.left != null) queue.add(root.left);
                if(root.right != null) queue.add(root.right);
                if(n == 0) res.add(root.val);  //将该层的最右一个节点添加到结果集中
            }
        }
        return res;
    }
}
210 / 210 test cases passed. Runtime: 2 ms  Your runtime beats 47.48% of javasubmissions.


import Queue
class Solution(object):
    def rightSideView(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        if root == None : return []
        queue, res = Queue.Queue(),[]
        queue.put(root)
        while not queue.empty() :
            n = queue.qsize()
            while (n > 0) :
                root = queue.get()
                if root.left : queue.put(root.left)
                if root.right : queue.put(root.right)
                if n == 1 : res.append(root.val)
                n -= 1
        return res
210 / 210 test cases passed. Runtime: 68 ms  Your runtime beats 15.41% of pythonsubmissions.

【思路】递归实现

采用前序遍历。采用 根->右->左 顺序进行遍历,并将每一层的第一个节点添加到结果集中:

public class Solution {
    public List<Integer> rightSideView(TreeNode root) {
        List<Integer> res = new ArrayList<Integer>();
        dfs(root, res, 0);
        return res;
    }
    private void dfs(TreeNode root, List<Integer> res, int level) {
        if (root == null) return;
        if (level == res.size()) {
            res.add(root.val);
        }
        dfs(root.right, res, level+1);
        dfs(root.left, res, level+1);
    }
}
210 / 210 test cases passed. Runtime: 1 ms  Your runtime beats 84.55% of javasubmissions.

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Leetcode 199 Binary Tree Right Side View 二叉树右视图

给出一个二叉树,设想你从树的右侧看过来,返回你从上往下可以看到的结点....

LeetCode-199. Binary Tree Right Side View (JAVA)(二叉树最右侧结点)

LeetCode-199. Binary Tree Right Side View (JAVA)(二叉树最右侧结点)

【LeetCode-面试算法经典-Java实现】【199-Binary Tree Right Side View(从右边看二叉树)】

【199-Binary Tree Right Side View(从右边看二叉树】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】代码下载【https://github.com/W...

[JAVA]LeetCode199 Binary Tree Right Side View

Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod...
  • fumier
  • fumier
  • 2015年04月09日 19:41
  • 685

LeetCode题解-199-Binary Tree Right Side View

原题 原题链接:https://leetcode.com/problems/binary-tree-right-side-view/ 解法概览 解法1:迭代法,BFS 解法2:...

[LeetCode]199.Binary Tree Right Side View

题目Given a binary tree, imagine yourself standing on the right side of it, return the values of the n...

[Leetcode] 199. Binary Tree Right Side View 解题报告

题目: Given a binary tree, imagine yourself standing on the right side of it, return the values o...

LeetCode - 199. Binary Tree Right Side View

题目要求写出Binary Tree从右侧看的情况,我们首先可以想到的是基于Level Order Traversal的方法,我们只需要将每一层最右侧的节点加入结果中即可,这种思路的代码如下: pub...

Leetcode 199. Binary Tree Right Side View

Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod...

Binary Tree Right Side View - LeetCode 199

题目描述: Given a binary tree, imagine yourself standing on the right side of it, return the values of ...
  • bu_min
  • bu_min
  • 2015年06月10日 19:26
  • 222
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode 199. Binary Tree Right Side View-树的右视图|递归|非递归|Java|Python
举报原因:
原因补充:

(最多只允许输入30个字)