Find Bottom Left Tree Value

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

答案

class Solution {
    public int findBottomLeftValue(TreeNode root) {
        if(root == null) return -1;
        Queue<TreeNode> q = new LinkedList<TreeNode>();
        q.offer(root);
        int candidate = -1;
        while(q.size() != 0) {
            int curr_size = q.size();
            for(int i = 0; i < curr_size; i++) {
                TreeNode t = q.poll();
                if(i == 0)
                    candidate = t.val;
                if(t.left != null) q.offer(t.left);
                if(t.right != null) q.offer(t.right);
            }
        }
        return candidate;
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,449评论 0 10
  • https://leetcode.com/problems/find-bottom-left-tree-value...
    DrunkPian0阅读 394评论 0 0
  • 状态栏的字体为黑色:UIStatusBarStyleDefault状态栏的字体为白色:UIStatusBarSty...
    离线0_0留言阅读 689评论 0 1
  • 在生命的旅程中,沾染上愤怒、仇恨时,一定要记得不时打扫心房,抛弃人生遗憾事,一笑而过。以淳朴应对虚伪,以善良化解仇...
    般若梵心阅读 339评论 0 0
  • 琅琊山风景名胜区位于安徽省东部滁州市境内,古名摩陀岭,系大别山向东延伸的一支余脉,后因东晋琅琊王避难于此,改称“琅...
    Assass1n阅读 159评论 0 1