本文共 1003 字,大约阅读时间需要 3 分钟。
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 binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public ListrightSideView(TreeNode root) { List result = new ArrayList(); if(null == root) return result; List result_left = rightSideView(root.left); List result_right = rightSideView(root.right); result.add(root.val); result.addAll(result_right); if(result_left.size() > result_right.size()){ result.addAll(result_left.subList(result_right.size()-1,result_left.size-1)); } return result; }}
转载地址:http://bvuni.baihongyu.com/