Find Bottom Left Tree Value
来源:互联网 发布:linux assert函数 编辑:程序博客网 时间:2024/06/05 05:06
第一篇文章,纪念自己的leetcode之路,这段时间的面试让我深深的知道自己coding能力的不足,沉下心来学习吧~
一. leetcode 513 Find Bottom Left Tree Value
这道题其实开始的时候也是想层次遍历,层次遍历真的是最简单的一种二叉树的遍历方式,因为逻辑很清晰不需要递归。本题只需要当深度首次+1的时候记录下节点即可。
于是第一种解法为
int findBottomLeftValue(TreeNode* root) {
int position;
TreeNode * result;
int num,i,level_num;
queue <TreeNode *> q; //2个队列,一个存点,一个存相应的level,操作是同时的
queue <int> level;
TreeNode * temp;
q.push(root);
level.push(0);
int m=-1; //这里的-1是为了输入【0】的时候的样例,原来我写的是0,过不去【0】
while(q.size())
{
temp = q.front();
q.pop();
position = level.front();
level.pop();
if(temp->left)
{
q.push(temp->left);
level.push(position+1);
//cout<<position+1<<endl;
}
if(temp->right)
{
q.push(temp->right);
level.push(position+1);
//cout<<position+1<<endl;
}
if(position>m) //当弹出的元素的层数首次大于之前保存的层数时,保存大的层数
{
m=position;
result = temp;
}
}
return result->val;
}
- Find Bottom Left Tree Value
- Find Bottom Left Tree Value
- Find Bottom Left Tree Value
- Find Bottom Left Tree Value
- Find Bottom Left Tree Value
- Find Bottom Left Tree Value
- Find Bottom Left Tree Value
- Find Bottom Left Tree Value
- Find Bottom Left Tree Value
- Find Bottom Left Tree Value
- Find Bottom Left Tree Value
- LeetCode 513. Find Bottom Left Tree Value
- 513:find bottom left tree value
- [leetcode]513. Find Bottom Left Tree Value
- 513. Find Bottom Left Tree Value
- 513. Find Bottom Left Tree Value
- [leetcode] 513. Find Bottom Left Tree Value
- 513. Find Bottom Left Tree Value
- 使用java反射机制动态调用javabean的get,set方法
- Python与人工神经网络(10)——神经网络可以干什么
- Dijkstra队列优化矩阵版
- Linux学习--gdb调试core文件
- 范型擦除中的原始类型类型接口--个人见解记录,非官方
- Find Bottom Left Tree Value
- Android TextView加载带有多张图片的HTML,并且解决图片造成的OOM
- 在linux下安装交叉编译链
- 工厂模式
- 欢迎使用CSDN-markdown编辑器
- ubuntu 安装软件的一些命令aptitude,apt-get
- org.apache.catalina.core.ContainerBase.addChildInternal ContainerBase.addChild: start: org.apache.
- 实现哈夫曼树
- Redis消息通知系统的实现