Leetcode 513 树左下角的值 DFS 与 BFS

  DFS 解法:

复制代码
 public final int findBottomLeftValue(TreeNode root) {
        if (root == null) return 0;
        return find(root, 0).node.val;
    }

    private final NodeAndDepth find(TreeNode node, int depth) {
        if (node.left == null && node.right == null) return new NodeAndDepth(depth, node);
        NodeAndDepth child = null;
        int nextDept = depth + 1;
        if (node.left != null) child = find(node.left, nextDept);
        if (node.right != null) {
            NodeAndDepth right = find(node.right, nextDept);
            if (child == null) child = right;
            else child = child.depth >= right.depth ? child : right;
        }
        return child;
    }

    private class NodeAndDepth {
        int depth;
        TreeNode node;

        NodeAndDepth(int depth, TreeNode node) {
            this.depth = depth;
            this.node = node;
        }
    }
复制代码

  BFS 解法:

复制代码
    public final int findBottomLeftValue(TreeNode root) {
        Queue<TreeNode> queue0 = new LinkedList<TreeNode>();
        Queue<TreeNode> queue1 = new LinkedList<TreeNode>();
        queue0.add(root);
        TreeNode re = null;
        while (!queue0.isEmpty() || !queue1.isEmpty()) {
            while (!queue0.isEmpty()) re = putChilds(queue0, queue1);
            while (!queue1.isEmpty()) re = putChilds(queue1, queue0);
        }
        return re==null?0:re.val;
    }

    private final TreeNode putChilds(Queue<TreeNode> queue0, Queue<TreeNode> queue1) {
        TreeNode re = queue0.peek();
        while (!queue0.isEmpty()) {
            TreeNode node = queue0.poll();
            if (node.left != null) queue1.add(node.left);
            if (node.right != null) queue1.add(node.right);
        }
        return re;
    }
复制代码

  JS DFS:

复制代码
var findBottomLeftValue = function (root) {
    if (!root) return 0;
    return find(root, 0).node.val;
};

var find = function (node, depth) {
    if (!node.left && !node.right) return new NodeAndDepth(node, depth);
    let re, nextDepth = depth + 1;
    if (node.left) re = find(node.left, nextDepth);
    if (node.right) {
        let right = find(node.right, nextDepth);
        if (!re) re = right;
        else re = re.depth >= right.depth ? re : right;
    }
    return re;
}

var NodeAndDepth = function (node, depth) {
    this.node = node;
    this.depth = depth;
}
复制代码

  JS BFS:

复制代码
var findBottomLeftValue = function (root) {
    if (!root) return 0;
    let queue = [root];
    let re = root;
    while (queue.length > 0) {
        re = queue[0];
        let len = queue.length;
        for (let i = 0; i < len; i++) {
            let node = queue.shift();
            if (node.left) queue.push(node.left);
            if (node.right) queue.push(node.right);
        }
    }
    return re.val;
}
复制代码

 

posted @   牛有肉  阅读(101)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示