cs: add “199. Binary Tree Right Side View”
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
f96b066323
commit
98dd458942
1 changed files with 34 additions and 0 deletions
34
cs/binary-tree-right-side-view.cs
Normal file
34
cs/binary-tree-right-side-view.cs
Normal file
|
@ -0,0 +1,34 @@
|
|||
/**
|
||||
* Definition for a binary tree node.
|
||||
* public class TreeNode {
|
||||
* public int val;
|
||||
* public TreeNode left;
|
||||
* public TreeNode right;
|
||||
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
|
||||
* this.val = val;
|
||||
* this.left = left;
|
||||
* this.right = right;
|
||||
* }
|
||||
* }
|
||||
*/
|
||||
public class Solution {
|
||||
private void GetView(List<int> levels, int level, TreeNode node) {
|
||||
if (node == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
while (level >= levels.Count) {
|
||||
levels.Add(0);
|
||||
}
|
||||
levels[level] = node.val;
|
||||
|
||||
GetView(levels, level + 1, node.left);
|
||||
GetView(levels, level + 1, node.right);
|
||||
}
|
||||
|
||||
public IList<int> RightSideView(TreeNode root) {
|
||||
var levels = new List<int>();
|
||||
GetView(levels, 0, root);
|
||||
return levels;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue