Binary Tree Right Side View
2015-06-07 10:30
246 查看
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,
You should return
Credits:
Special thanks to @amrsaqr for adding this problem and creating all test cases.
Breadth-First-Search
using a queue to store tree nodes of every level, and add the last tree node value of every level into result.
/**
* 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> ret = new ArrayList<Integer>();
if (root == null)
return ret;
Queue<TreeNode> queue= new LinkedList<TreeNode>();
queue.offer(root);
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; i++) {
TreeNode node = queue.poll();
if (node.left != null)
queue.offer(node.left);
if (node.right != null)
queue.offer(node.right);
if (i == size - 1)
ret.add(node.val);
}
}
return ret;
}
}
Depth-First-Search
/**
* 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> ret = new ArrayList<Integer>();
helper(ret, root, 1);
return ret;
}
private void helper(List<Integer> ret, TreeNode root, int level) {
if (root == null)
return;
if (level > ret.size())
ret.add(root.val);
helper(ret, root.right, level + 1);
helper(ret, root.left, level + 1);
}
}
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].
Credits:
Special thanks to @amrsaqr for adding this problem and creating all test cases.
Breadth-First-Search
using a queue to store tree nodes of every level, and add the last tree node value of every level into result.
/**
* 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> ret = new ArrayList<Integer>();
if (root == null)
return ret;
Queue<TreeNode> queue= new LinkedList<TreeNode>();
queue.offer(root);
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; i++) {
TreeNode node = queue.poll();
if (node.left != null)
queue.offer(node.left);
if (node.right != null)
queue.offer(node.right);
if (i == size - 1)
ret.add(node.val);
}
}
return ret;
}
}
Depth-First-Search
/**
* 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> ret = new ArrayList<Integer>();
helper(ret, root, 1);
return ret;
}
private void helper(List<Integer> ret, TreeNode root, int level) {
if (root == null)
return;
if (level > ret.size())
ret.add(root.val);
helper(ret, root.right, level + 1);
helper(ret, root.left, level + 1);
}
}
相关文章推荐
- mysql主从复制
- iOSProgramming: The Big Nerd Ranch Guide (4th Edition) 阅读笔记-关于 View 的 Redrawing 和 Run Loop
- Path
- 360初赛溢出题
- ServletContext 与application的异同
- redis 安装
- WPS2012交叉引用提示word比wps这种强烈的更新参考
- 清除windows 系统中 任意已安装任意版本的 .NET Framework
- 清除windows 系统中 任意已安装任意版本的 .NET Framework
- 让Web API支持OData查询
- RESTier:构建OData服务的新框架
- CA服务器的升级 2003—2012
- hdu 5212 Code
- NetBeans java 编译时提示使用了未经检查或不安全的操作
- Android25闹钟项目——源码下载
- 8.噪音和错误
- Leetcode Number of 1 bits
- ios xmpp开发应用后台模式接收聊天信息
- (6)循环技巧
- Javascript 数组函数