leetcode 513 Find Bottom Left Tree Value C++
来源:互联网 发布:程序员培训机构 编辑:程序博客网 时间:2024/05/02 04:34
一次AC,用的层次遍历,遍历一层的时候记录第一个node
int findBottomLeftValue(TreeNode* root) { queue<TreeNode *>que; que.push(root); TreeNode *leftNode = NULL; int horizinal = 1; int tmpHorizinal = 0; int sum = 0; while(!que.empty()) { TreeNode *node = que.front(); sum++; que.pop(); if (sum == 1) { leftNode = node; } if(node->left) { que.push(node->left); tmpHorizinal++; } if(node->right) { que.push(node->right); tmpHorizinal++; } if (sum == horizinal) { horizinal = tmpHorizinal; tmpHorizinal = 0; sum = 0; } } return leftNode->val; }后来看了discuss,发现这么写更好,思路是一样的。
int findBottomLeftValue(TreeNode* root) { queue<TreeNode *>que; que.push(root); TreeNode *leftNode = NULL; while(!que.empty()) { int size = que.size(); leftNode = que.front(); while(size--) { TreeNode *node = que.front(); que.pop(); if(node->left) que.push(node->left); if(node->right) que.push(node->right); } } return leftNode->val; }
0 0
- LeetCode #513: Find Bottom Left Tree Value
- Leetcode 513 Find Bottom Left Tree Value
- leetcode 513 Find Bottom Left Tree Value
- LeetCode#513 Find Bottom Left Tree Value
- LeetCode 513. Find Bottom Left Tree Value (C++)
- 【leetcode】513. Find Bottom Left Tree Value(Python & C++)
- LeetCode 513. Find Bottom Left Tree Value
- [leetcode]513. Find Bottom Left Tree Value
- [leetcode] 513. Find Bottom Left Tree Value
- 【LeetCode】513. Find Bottom Left Tree Value
- Leetcode 513. Find Bottom Left Tree Value
- LeetCode 513. Find Bottom Left Tree Value
- LeetCode 513. Find Bottom Left Tree Value
- 【leetcode】 513. Find Bottom Left Tree Value
- 513. Find Bottom Left Tree Value | LeetCode
- leetcode-513. Find Bottom Left Tree Value
- LeetCode 513. Find Bottom Left Tree Value
- leetcode 513. Find Bottom Left Tree Value
- 家庭理财规划之一
- 子网划分知识点及题目
- javaScript笔记
- R语言学习之基础知识二
- 解决spring boot中swagger-ui.html访问404以及配置全局header
- leetcode 513 Find Bottom Left Tree Value C++
- 329. Longest Increasing Path in a Matrix(第七周)
- 引用
- 简单实现四舍五入的好方法
- -日志- Fastjson 总结
- Java多线程详解
- Longest Palindrome
- hibernate之Criteria查询对象
- 多线程之join()方法---(Thread提供的让一个线程等待另一个线程完成的方法)