Skip to content

Latest commit

 

History

History
56 lines (42 loc) · 1.48 KB

513.md

File metadata and controls

56 lines (42 loc) · 1.48 KB

2019-05-07 吴亲库里 库里的深夜食堂


✏️描述

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


✏️题目实例

✏️题目分析

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

   
   /**
    * 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;
           
       }
   }

联系