当前位置:网站首页>Leetcode-199 - right view of binary tree
Leetcode-199 - right view of binary tree
2022-04-23 09:03:00 【z754916067】
subject

Ideas
- I've done a problem to distinguish each layer of binary tree traversal before , This problem should be based on that , Just add the rightmost node of each layer to the returned list . I wrote it directly AC. Look at the problem solution, which belongs to the solution of breadth first traversal , Then let's look at the solution of depth first traversal .
Code (BFS)
public List<Integer> rightSideView(TreeNode root) {
List<Integer> ans = new ArrayList<>();
// Create a queue
Queue<TreeNode> que = new ArrayDeque<>();
que.add(root);
while(!que.isEmpty()){
// The length of the current layer
int size = que.size();
// Each floor comes out together
for(int i=0;i<size;i++){
TreeNode temp = que.remove();
if(i== size-1){
// The last words Need to add ans
ans.add(temp.val);
}
// Enter it around the child
if(temp.left!=null) que.add(temp.left);
if(temp.right!=null) que.add(temp.right);
}
}
// return ans
return ans;
}
Code (DFS)
List<Integer> ans = new ArrayList<>();
public List<Integer> rightSideView(TreeNode root) {
// The root is traversed from right to left It can ensure that the node on the right is traversed first
// Need to maintain a depth To determine whether to join ans
DFS(root,0);
return ans;
}
public void DFS(TreeNode root,int depth){
if(root==null) return;
// If at present depth and ans equal Note No depth The nodes of the layer have not been added ans
// And as root right left traversal At this time root It must be the rightmost node and should be added
if(depth==ans.size()) ans.add(root.val);
// Continue traversing
DFS(root.right,depth+1);
DFS(root.left,depth+1);
}
版权声明
本文为[z754916067]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230722125313.html
边栏推荐
- Redis Desktop Manager for Mac
- How does kubernetes use harbor to pull private images
- 请提前布局 Star Trek突破链游全新玩法,市场热度持续高涨
- [boutique] using dynamic agent to realize unified transaction management II
- Learn SQL injection in sqli liabs (Level 11 ~ level 20)
- npm ERR! network
- Go language self-study series | golang structure pointer
- Star Trek's strong attack opens the dream linkage between metacosmic virtual reality
- Pctp test experience sharing
- Trc20 fund collection solution based on thinkphp5 version
猜你喜欢

Four pictures to understand some basic usage of Matplotlib

Automatic differentiation and higher order derivative in deep learning framework

L2-3 romantic silhouette (25 points)

Valgrind et kcachegrind utilisent l'analyse d'exécution

请提前布局 Star Trek突破链游全新玩法,市场热度持续高涨

Star Trek's strong attack opens the dream linkage between metacosmic virtual reality

2022-04-22 openebs cloud native storage

使用flask和h5搭建网站/应用的简要步骤

Introduction to GUI programming swing

Valgrind and kcache grind use run analysis
随机推荐
Four pictures to understand some basic usage of Matplotlib
Applet in wechat and app get current ()
Brush classic topics
Introduction to matlab
Research purpose, construction goal, construction significance, technological innovation, technological effect
Go language self-study series | golang structure as function parameter
Arbre de dépendance de l'emballage des ressources
Idea package jar file
请提前布局 Star Trek突破链游全新玩法,市场热度持续高涨
在sqli-liabs学习SQL注入之旅(第十一关~第二十关)
Chris LATTNER, father of llvm: the golden age of compilers
Introduction to GUI programming swing
The K neighbors of each sample are obtained by packet switching
Flink reads MySQL and PgSQL at the same time, and the program will get stuck without logs
How to read excel table to database
Search tree judgment (25 points)
深度学习框架中的自动微分及高阶导数
Flink同时读取mysql与pgsql程序会卡住且没有日志
To remember the composition ~ the pre order traversal of binary tree
Project upload part