Light is falling on a tree from left side you have to find all the nodes on which this light will fa
来源:互联网 发布:大连民族学院网络 编辑:程序博客网 时间:2024/05/16 02:13
思路:所求节点就是层序遍历中,每一层的第一个节点。
#include <vector>#include <queue>#include <utility> // pairusing namespace std;struct Node{ int data; Node* left; Node* right;};vector<Node*> lightFall(Node* root){ vector<Node*> result; // store the nodes on which the light will fall on queue< pair<Node*, int> > que; que.push( pair<Node*, int>(root, 0) ); // First, push the root node int level = 0; // the level of the nodes while(que.size()>0) { pair<Node*, int> frontPair = que.front(); que.pop(); if(level == frontPair.second) // only push back the first node of each level { result.push_back(frontPair.first); level++; } if(frontPair.first->left) que.push( pair<Node*, int>( (frontPair.first)->left, frontPair.second+1) ); if(frontPair.first->right) que.push( pair<Node*, int>( (frontPair.first)->right, frontPair.second+1) ); }}
注意:first和second是pair 的两个member variable,而不是member function。
0 0
- Light is falling on a tree from left side you have to find all the nodes on which this light will fa
- Finally,Which light is on?
- This view is not constrained vertically: at runtime it will jump to the left unless you add a vertic
- INS-35354] The system on which you are attempting to install Oracle RAC is not part of a valid clust
- Unable to start debugging on the web server. You do not have permission to debug the application. The URL for this project is in
- AS- This view is not constrained vertically. At runtime it will jump to the left/(0,0) unless you
- You don't have permission to access / on this server
- You don't have permission to access /*** on this server
- You don't have permission to access / on this server.
- You don't have permission to access on this server.
- You don't have permission to access / on this server.
- You don't have permission to access ××× on this server.
- You don't have permission to access / on this server
- You don't have permission to access / on this server.
- You don’t have permission to access/on this server
- You don't have permission to access / on this server.
- You don't have permission to access / on this server.
- You don't have permission to access / on this server.
- 持续集成工具
- Binary Tree Level Order Traversal Leetcode Python
- 堆结构(三) - 斜堆的原理与实现
- 自主的文件缓存组件FileCache和被封装的System.Net.Cache
- 图结构(一)- 图的理论基础
- Light is falling on a tree from left side you have to find all the nodes on which this light will fa
- 103. Binary Tree Zigzag Level Order Traversal Leetcode Python
- GDB调试之重定向
- EventBus小记
- Leetcode NO.73 Set Matrix Zeroes
- CentOS yum有时出现“Could not retrieve mirrorlist ”的解决办法——resolv.conf的配置
- ContentProvider 内容提供者
- 它怎么又去了。
- 当前商务日语教材的现状及其建设建议