标签:
题目描述:/** * 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) { if(root == null){ return new List<int>(); } var result = new List<int>(); RightView(new List<TreeNode>(){root}, result); return result; } public void RightView(IList<TreeNode> nodes, IList<int> result) { if(!nodes.Any()){ return; } // add last node which is on the most right position result.Add(nodes.Last().val); // BFS var children = new List<TreeNode>(); foreach(var n in nodes){ var nl = Children(n); if(nl.Any()){ children.AddRange(nl); } } RightView(children, result); } private IList<TreeNode> Children(TreeNode node){ if(node == null){ return new List<TreeNode>(); } var list = new List<TreeNode>(); if(node.left != null){ list.Add(node.left); } if(node.right != null){ list.Add(node.right); } return list; } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
LeetCode -- Binary Tree Right Side View
标签:
原文地址:http://blog.csdn.net/lan_liang/article/details/48575915