/**
* 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> list = new ArrayList<>();
if(root == null) return list;
helper(list, 0, root);
return list;
}
private void helper(List<Integer> list, int depth, TreeNode root){
if(root == null){
return;
}
if(depth == list.size()){
list.add(root.val);
}
helper(list, depth + 1, root.right);
helper(list, depth + 1, root.left);
}
}
[LeetCode] 199. Binary Tree Right Side View
最新推荐文章于 2023-11-07 09:51:46 发布