【LeetCode】513. Find Bottom Left Tree Value

问题描述

问题链接:https://leetcode.com/problems/find-bottom-left-tree-value/#/description

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.

我的代码

思路探索

使用递归是肯定的,关键是确定递归结束的条件,和取舍标准。

递归的结束条件在于没有子节点的时候就结束递归,返回自己的值和高度。

如果一个节点既有左节点又有右节点,就检查两个子节点的递归结果。如果左子节点的高度大于或者等于右子节点的高度,返回左子节点的最左子节点的值,否则返回右子节点的值。

如果一个节点只有左节点或者右节点,返回子节点的最左节点的值。

唉,这段好绕口,直接看下面的代码和伪码吧。

通过的代码

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    private static class SearchBean {
        public int value;
        public int deep;

        public SearchBean(){}

        public SearchBean(int value,int deep){
            this.value = value;
            this.deep = deep;
        }
    }

    public int findBottomLeftValue(TreeNode root) {
        /*
        思路是使用递归,写个伪码吧,比较好表达
        def func(root, deep){
            if root have left child or root have right child:
                leftDeep,leftValue = func(root.leftChild, deep + 1)
                rightDeep,rightValue = func(root.rightChild, deep + 1)

                if leftDeep >= rightDeep:
                    return leftDeep,leftValue
                else:
                    return rightDeep,rightValue
            else:
                return deep,root.val
        }
        */
        return func(root,1).value;
    }

    private SearchBean func(TreeNode root, int deep){
        if(root.left == null && root.right == null){
            return new SearchBean(root.val,deep);
        }
        SearchBean leftBean = root.left != null ? func(root.left,deep + 1) : new SearchBean(-1,-1);
        SearchBean rightBean = root.right != null ? func(root.right,deep + 1) : new SearchBean(-1,-1);

        if(leftBean.deep >= rightBean.deep){
            return leftBean;
        }
        return rightBean;
    }
}

我的代码打败了59.36%的Java代码。去讨论区学习学习。

讨论区

Right-to-Left BFS (Python + Java)

链接地址:https://discuss.leetcode.com/topic/78981/right-to-left-bfs-python-java

这个思路非常巧妙,值得好好膜拜一个。

Doing BFS right-to-left means we can simply return the last node’s value and don’t have to keep track of the first node in the current row or even care about rows at all. Inspired by @fallcreek’s solution (not published) which uses two nested loops to go row by row but already had the right-to-left idea making it easier. I just took that further.

代码如下:

public int findLeftMostNode(TreeNode root) {
    Queue<TreeNode> queue = new LinkedList<>();
    queue.add(root);
    while (!queue.isEmpty()) {
        root = queue.poll();
        if (root.right != null)
            queue.add(root.right);
        if (root.left != null)
            queue.add(root.left);
    }
    return root.val;
}

理解这段代码关键在于理解两个前提:

  1. 在上一层的节点没有被访问完的情况下不会访问下一层的节点
  2. 每一层的节点是从右向左依次访问的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值