• 513找树左下角值


    给定一个二叉树的 根节点 root,请找出该二叉树的 最底层 最左边 节点的值。

    假设二叉树中至少有一个节点。

    示例 1:

    输入: root = [2,1,3]
    输出: 1
    

    示例 2:

    输入: [1,2,3,4,null,5,6,null,null,7]
    输出: 7
    
    1. class Solution {
    2. public:
    3. int findBottomLeftValue(TreeNode* root) {
    4. //层序迭代
    5. //记录每一行的第一个元素,从左往右
    6. //或者从右往左,最后一个遍历的就是ans
    7. queueque;
    8. int ans = 0;
    9. if(!root) return 0;
    10. que.push(root);
    11. while(!que.empty()){
    12. int size = que.size();
    13. while(size--){
    14. TreeNode* node = que.front();
    15. que.pop();
    16. if(node->right) que.push(node->right);
    17. if(node->left) que.push(node->left);
    18. ans = node->val;
    19. }
    20. }
    21. return ans;
    22. }
    23. };
    24. //迭代bfs,从左往右遍历
    25. class Solution {
    26. public:
    27. int findBottomLeftValue(TreeNode* root) {
    28. //层序迭代
    29. //记录每一行的第一个元素,从左往右
    30. //或者从右往左,最后一个遍历的就是ans
    31. queueque;
    32. int ans = 0;
    33. if(!root) return 0;
    34. que.push(root);
    35. while(!que.empty()){
    36. int size = que.size();
    37. for(int i = 0;i < size;i++){
    38. TreeNode* node = que.front();
    39. que.pop();
    40. if(i == 0) ans = node->val;
    41. if(node->left) que.push(node->left);
    42. if(node->right) que.push(node->right);
    43. }
    44. }
    45. return ans;
    46. }
    47. };
    48. //dfs
    49. class Solution {
    50. public:
    51. int maxnum = INT32_MIN;
    52. int res = 0;
    53. void dfs(TreeNode* root,int depth){
    54. if(!root) return;
    55. if(!root->left && !root->left){
    56. if(depth > maxnum){
    57. maxnum = depth;
    58. res = root->val;
    59. }
    60. }
    61. if(root->left) dfs(root->left,depth+1);//此处隐藏着回溯
    62. //第二层,d=1。符合到第三层 d=2,不符合,然后max记录一下,val记录
    63. //回到第二层,在回到第一层,d=0。进入右边第一层同理。
    64. if(root->right) dfs(root->right,depth+1);
    65. }
    66. int findBottomLeftValue(TreeNode* root) {
    67. //dfs
    68. int depth = 0;
    69. if(!root) return 0;
    70. dfs(root,depth);
    71. return res;
    72. }
    73. };
    74. //dfs,第二个版本
    75. class Solution {
    76. public:
    77. int maxnum = INT32_MIN;
    78. int res;
    79. void dfs(TreeNode* root,int depth){
    80. if(!root->left && !root->right){
    81. if(depth > maxnum){
    82. maxnum = depth;
    83. res = root->val;
    84. }
    85. return;
    86. }
    87. if(root->left){
    88. depth++;
    89. //在这里dep改变了。而dfs(root->left,dep+1),dep//并没有改变,改变的只是传入下一层的dep变化,并没有改变当前层。
    90. dfs(root->left,depth);
    91. depth--; // 回溯。得到多少,为了符合条件就得减掉多少
    92. }
    93. if(root->right){
    94. depth++;
    95. dfs(root->right,depth);
    96. depth--;
    97. }
    98. }
    99. int findBottomLeftValue(TreeNode* root) {
    100. //dfs
    101. int depth = 0;
    102. dfs(root,depth);
    103. return res;
    104. }
    105. };

  • 相关阅读:
    Java lambda 动态查询
    LeetCode in Python 10. Regular Expression Matching (正则表达式匹配)
    Java中实现双向链表
    塔防海岸线用户协议
    安卓机调用 audio.play()时 报错:API can only be initiated by a user gesture
    流量狂飙!暴涨2000万播放成B站创作标杆
    汽车租赁管理系统的设计与实现(JSP+SqlServer在线租车网站)
    vue3+ts+vite数据大屏自适应总结(两种方法)
    hive substr用法
    借鉴前端事件机制的Spring AOP
  • 原文地址:https://blog.csdn.net/qq_63819197/article/details/133247088