首页 > 其他分享 >199. Binary Tree Right Side View

199. Binary Tree Right Side View

时间:2022-11-23 23:45:47浏览次数:43  
标签:Binary Right curr helper result 199 null root currDepth

Given the root of 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.

 

Example 1:

Input: root = [1,2,3,null,5,null,4]
Output: [1,3,4]

public List<Integer> rightSideView(TreeNode root) {
List<Integer> result = new ArrayList<Integer>();
helper(root, result, 0);
return result;
}

public void helper(TreeNode curr, List<Integer> result, int currDepth) {
if (curr == null) {
return;
}
if (currDepth == result.size()) {
result.add(curr.val);
}
helper(curr.right, result, currDepth + 1);
helper(curr.left, result, currDepth + 1);
}

标签:Binary,Right,curr,helper,result,199,null,root,currDepth
From: https://www.cnblogs.com/MarkLeeBYR/p/16920551.html

相关文章