LeetCode题目-513

首页 编程分享 LEET_CODE 正文

leetCode 转载 编程分享 2021-11-25 00:14:23

简介 LeetCode题目-513



✏描述

给定一个二叉树,找出树最后一层最左边的结点值。


✏题目实例

✏题目分析

使用层进行遍历,每个层从右往左遍历,这样就能保证我们最后输出的一定是 最后一层的最左结点。

   
   /**
    * Definition for a binary tree node.
    * class TreeNode {
    *     public $val = null;
    *     public $left = null;
    *     public $right = null;
    *     function __construct($value) { $this->val = $value; }
    * }
    */
   class Solution {
   
       /**
        * @param TreeNode $root
        * @return Integer
        */
       function findBottomLeftValue($root) {
           $data=[];
           array_unshift($data,$root);
           while(!empty($data)){
               $node=array_shift($data);
               if($node->right !=null) array_push($data,$node->right);
               if($node->left !=null) array_push($data,$node->left);
           }
           return $node->val;
           
       }
   }

转载链接:https://leetcode.cn/


Tags:


本篇评论 —— 揽流光,涤眉霜,清露烈酒一口话苍茫。


    声明:参照站内规则,不文明言论将会删除,谢谢合作。


      最新评论




ABOUT ME

Blogger:袅袅牧童 | Arkin

Ido:PHP攻城狮

WeChat:nnmutong

Email:nnmutong@icloud.com

标签云