• [leetcode] Find Bottom Left Tree Value


    Given a binary tree, find the leftmost value in the last row of the tree.

    Example 1:

    Input:
    
        2
       / 
      1   3
    
    Output:
    1
    

    Example 2: 

    Input:
    
            1
           / 
          2   3
         /   / 
        4   5   6
           /
          7
    
    Output:
    7
    

    Note: You may assume the tree (i.e., the given root node) is not NULL.


    分析:题目翻译一下:要求寻找一个二叉树最底层,最左边的数字。
    第一个思路:想到用BFS,每层遍历。保存每层的结果,并且想办法找到最后一层的最左边的数字。代码如下:
     1 class Solution {
     2     public int findBottomLeftValue(TreeNode root) {
     3         //BFS method
     4         Queue<TreeNode> stack = new LinkedList<>(); //用队列实现层次遍历
     5         List<Integer> list = new ArrayList<>(); //用list保存每个层结果,如果有下一层,就覆盖
     6 
     7         int length = 0;
     8         stack.offer(root);
     9         while ( !stack.isEmpty() ){
    10             length = stack.size();
    11             for ( int i = 0 ; i < length ; i ++ ) {
    12                 TreeNode t = stack.poll();
    13                 list.add(t.val);
    14                 if (t.left != null) stack.offer(t.left);
    15                 if (t.right != null) stack.offer(t.right);
    16             }
    17         }
    18         System.out.println(list);
    19         return list.get(list.size()-length);
    20     }
    21 }

      运行时间12ms,击败3.74%。显然是个不好的方法,因为使用了list这个多余的空间。下面想办法优化。

    第二个思路:如果不用list,直接返回最后一个元素呢。这个时候想到层次遍历需要从右往左遍历,这样最后一个访问的元素就是最后一层最左边的了。

     1 class Solution {
     2     public int findBottomLeftValue(TreeNode root) {
     3         //BFS method
     4         Queue<TreeNode> stack = new LinkedList<>(); //用队列实现层次遍历
     5 
     6         int result = root.val;
     7         stack.offer(root);
     8         while ( !stack.isEmpty() ){
     9             int length = stack.size();
    10             for ( int i = 0 ; i < length ; i ++ ) {
    11                 TreeNode t = stack.poll();
    12                 result = t.val;
    13                 
    14                 if (t.right != null) stack.offer(t.right);
    15                 if (t.left != null) stack.offer(t.left);
    16             }
    17         }
    18         //System.out.println(list);
    19         return result;
    20     }
    21 }

      运行时间7ms,这应该是非递归方法最快的了。

    思路三:二叉树类问题,肯定也是可以用递归来做的。

    因为要判断最底层,所以要增加一个level判断第几层。用递归最关键就是找到最左边元素的判断,这里非常巧妙用一个level判断。因为递归都是从左边开始递归的,因此用一个level变量记录当前遍历到的最左边的元素位于第几层。

     1 class Solution {
     2     int res = 0;
     3     int level = 0;
     4     public int findBottomLeftValue(TreeNode root) {
     5         res = root.val;
     6         helper(root,0);
     7         return res;
     8     }
     9 
    10     private void helper(TreeNode root, int curlevel) {
    11         if ( root == null ) return;
    12         if ( root.left == null && root.right == null ) {
    13             res = curlevel > level?root.val:res;
    14             level = Math.max(level,curlevel);
    15         }
    16         helper(root.left,curlevel+1);
    17         helper(root.right,curlevel+1);
    18     }
    19 }

      运行时间5ms。这个递归核心就在于如何判断是不是最左边的。

  • 相关阅读:
    一个不错的资源共享微盘
    LUA upvalue使用陷阱一例
    安卓破解视频教程合集
    开发Android逆向工具
    手机上编程,编写android apk
    smail修改字符串 汉字
    Android logcat命令详解
    protobuf
    Android Rxjava
    butterknife
  • 原文地址:https://www.cnblogs.com/boris1221/p/9743314.html
Copyright © 2020-2023  润新知