Shu-How Zの小窝

Loading...

LeetCode:111.二叉树的最小深度

LeetCode:111.二叉树的最小深度

解题思路求最小深度,考虑使用广度优先遍历。在广度优先遍历过程中,遇到叶子节点,停止遍历,返回节点层级。

解题步骤广度优先遍历整棵树,并记录每个节点的层级。遇到叶子节点,返回节点层级,停止遍历。

//dfs
var minDepth = function(root) {
    if (!root) return 0;
    function dfs(node) {
        if (!node.left && !node.right) return 1; // 叶子节点,返回深度1
        let leftDepth = Infinity, rightDepth = Infinity;
        if (node.left) leftDepth = dfs(node.left);
        if (node.right) rightDepth = dfs(node.right);
        return Math.min(leftDepth, rightDepth) + 1;
    }
    return dfs(root);
};

var minDepth = function(root) {
    if (!root) return 0;
    let queue = [[root, 1]];
    while(queue.length) {
        let [n,length]=queue.shift();
        if(!n.left&&!n.right) return length;
        if(n.left){queue.push([n.left,length+1])}
        if(n.right){queue.push([n.right,length+1])}
    }
};
// time 2ms   space 83.99mb  O(n)

var minDepth = function(root) {
    if (!root) return 0;
    let queue = [{n:root,length:1}];
    while(queue.length) {
        let {n,length}=queue.shift();
        if(!n.left&&!n.right) return length;
        if(n.left){queue.push({n:n.left,length:length+1})}
        if(n.right){queue.push({n:n.right,length:length+1})}
    }
};
// time 4ms   space 83.96mb  O(n)

// time 7ms space 85.56mb
var minDepth = function(root) {
    if (!root) return 0;
    let s=Symbol('root')
    let map=new Map()
    map.set(s,{n:root,length:1})
    let key
    let n,length
    while(map.size>0) {
        key=Array.from(map.keys())[0]
        n=map.get(key).n
        length=map.get(key).length
        map.delete(key);
        if(!n.left&&!n.right) return length;
        if(n.left){map.set(Symbol(),{n:n.left,length:length+1})}
        if(n.right){map.set(Symbol(),{n:n.right,length:length+1})}
    }
};

//time 10ms  space 83.66mb
var minDepth = function(root) {
    if (!root) return 0;
    let s=Symbol('root')
    let map=new Map()
    let weakMap=new WeakMap()
    weakMap.set(s,{n:root,length:1})
    map.set(s,weakMap)
    let key
    let n,length
    while(map.size>0) {
        key=Array.from(map.keys())[0];
        n=map.get(key).get(key).n;
        length=map.get(key).get(key).length;
        map.delete(key);weakMap.delete(key);
        if(!n.left&&!n.right) return length;
        if(n.left){let l=Symbol();map.set(l,weakMap.set(l,{n:n.left,length:length+1}))}
        if(n.right){let r=Symbol();map.set(r,weakMap.set(r,{n:n.right,length:length+1}))}
    }
    map=null
    weakMap=null
};

//time 14ms  space 86mb
var minDepth = function(root) {
    if (!root) return 0;
    
    let minDepth = Infinity;
    
    function preorder(node, depth) {
        if (!node) return;
        
        // If it's a leaf node
        if (!node.left && !node.right) {
            minDepth = Math.min(minDepth, depth);
        }
        
        preorder(node.left, depth + 1);
        preorder(node.right, depth + 1);
    }
    
    preorder(root, 1);
    
    return minDepth;
};

//只用map 和 一个symbol 不通过....error simple
//可能只能3个symbol----
var minDepth = function(root) {
    if (!root) return 0;
    
    let s = Symbol('node');
    let map = new Map();
    map.set(s, { node: root, depth: 1 });
    
    while (map.size > 0) {
        let { node, depth } = map.get(s);
        map.delete(s);
        
        if (!node.left && !node.right) {
            return depth;
        }
        
        if (node.left) {
            map.set(s, { node: node.left, depth: depth + 1 });
        }
        
        if (node.right) {
            // If left child exists, we need to process it first
            if (!map.has(s)) {
                map.set(s, { node: node.right, depth: depth + 1 });
            }
        }
    }
};

6/3

posted @ 2025-01-12 16:43  KooTeam  阅读(11)  评论(0)    收藏  举报