2019独角兽企业重金招聘Python工程师标准>>>
原题
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].
题目大意
给定一个二叉树&#xff0c;想象自己站在树的右边&#xff0c;返回从下到下你能看到的节点的值。
解题思路
二叉树的层次遍历&#xff0c;每层按照从左向右的顺序依次访问节点&#xff0c;&#xff08;每一层取最右边的结点&#xff09;
代码实现
树结点类
public class TreeNode {int val;TreeNode left;TreeNode right;TreeNode(int x) {val &#61; x;}
}
算法实现类
public class Solution {public List
}