LeetCode-199.Binary Tree Right Side View

原创 2016年05月30日 22:04:47

https://leetcode.com/problems/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].

很简单的一道题,跟一层层遍历树的题型一样

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left;
 *     public TreeNode right;
 *     public TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public IList<int> RightSideView(TreeNode root) 
    {
        IList<int> res = new List<int>();
        if (root == null)
            return res;
        Queue<TreeNode> q = new Queue<TreeNode>();
        q.Enqueue(root);
        int n;
        TreeNode node;
        while ((n=q.Count)>0)
        {
            res.Add(q.Peek().val);
            for (int i = 0; i < n; i++)
            {
                node = q.Dequeue();
                if (node.right != null)
                    q.Enqueue(node.right);
                if (node.left != null)
                    q.Enqueue(node.left);
            }
        }
        return res;
    }
}

C++实现

#include <vector>
#include <queue>
using namespace std;
struct TreeNode
{
	int val;
	TreeNode *left;
	TreeNode *right;
	TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

vector<int> rightSideView(TreeNode* root)
{
	vector<int> res;
	if (!root)
		return res;
	queue<TreeNode*> q;
	q.push(root);
	int n;
	TreeNode* node;
	while ((n = q.size()) > 0)
	{
		res.push_back(q.front()->val);
		for (int i = 0; i < n; i++)
		{
			node = q.front();
			q.pop();
			if (node->right)
				q.push(node->right);
			if (node->left)
				q.push(node->left);
		}
	}
	return res;
}



递归解,参考https://leetcode.com/discuss/31348/my-simple-accepted-solution-java

public class Solution
{
    public IList<int> RightSideView(TreeNode root) 
    {
        IList<int> res = new List<int>();
        Func(res, root, 0);
        return res;
    }
    
    private void Func(IList<int> res, TreeNode root, int level)
    {
        if (root == null)
            return;
        if (level == res.Count)
            res.Add(root.val);
        Func(res,root.right,level + 1);
        Func(res,root.left,level + 1);
    }
}



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

相关文章推荐

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

原题链接:199. Binary Tree Right Side View 【思路】非递归实现 层次遍历,每一层都是从左到右遍历,将每一层的最右一个节点添加到结果集中就是我们需要得到的结果: publ...

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

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

我是如何成为一名python大咖的?

人生苦短,都说必须python,那么我分享下我是如何从小白成为Python资深开发者的吧。2014年我大学刚毕业..

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

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

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

题目:给定一棵二叉树,每个节点的信息包括左右节点的指针,以及其本身的值。让你给出二叉树的“右视图”, 即从上至下给出每一层最右边的节点。           这题可以用宽度优先搜索的方法来做,具体实...

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

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

[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

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

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

【199-Binary Tree Right Side View(从右边看二叉树】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】代码下载【https://github.com/W...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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