二叉树的最小深度

时间:2014-06-17 07:11:09

标签: binary-tree binary-search-tree

我在读二元树。在练习编码问题时,我遇到了一些解决方案,要求找到二叉树的最小深度。 现在根据我的理解,深度不是从根到节点的边缘(叶节点/二叉树的情况下的叶节点)

二叉树的最小深度是什么{1,2}

根据我的解决方案,它应该是1.

10 个答案:

答案 0 :(得分:5)

我的测试解决方案

public int minDepth(TreeNode root) {
    if(root == null){
        return 0;
    }
    int ldepth = minDepth(root.left);
    int rdepth = minDepth(root.right);
    if(ldepth == 0){
        return 1+rdepth;
    }else if(rdepth == 0){
        return 1+ldepth;
    }

    return (1 + Math.min(rdepth, ldepth));
}

这里,我们计算节点的ldepth(最小左子树深度)和rdepth(最小右子树深度)。然后,如果ldepth为零但rdepth不是,则表示当前节点不是叶节点,因此返回1 + rdepth。如果rdepth和ldepth都是零,那么仍然是“如果'条件工作,因为我们为当前叶节点返回1 + 0。

其他类似的逻辑如果'科。在'返回'声明为“如果'条件已经失败,我们返回1(当前节点)+左右分支的递归调用的最小值。

答案 1 :(得分:2)

请记住,叶节点既没有左子也没有右子。

  1
  /
 /
2

所以这里2是叶节点但是1不是。因此,假设根节点的深度为1,则此情况的最小深度为2。

#include<vector>
#include<iostream>
#include<climits>
using namespace std;

  struct TreeNode {
      int val;
      TreeNode *left;
      TreeNode *right;
      TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  };


class Solution {
public:
    int minDepth(TreeNode *root) {

        if(root == NULL) return 0;
        return getDepth(root);
    }
    int getDepth(TreeNode *r ){
        if(r == NULL) return INT_MAX;
        if(r->left == NULL && r->right == NULL)
            return 1;
        return 1+ min(getDepth(r->left), getDepth(r->right));
    }
};

答案 2 :(得分:1)

二叉树的深度是从根到叶的最长路径的长度。在我看来,深度应该是2。

答案 3 :(得分:1)

public int minDepth(TreeNode root){
        if(root==null)
            return 0;
        else if(root.left==null && root.right==null)
            return 1;
        else if(root.left==null)
            return 1+minDepth(root.right);
        else if(root.right==null) 
            return 1+minDepth(root.left);
        else
        return 1+Math.min(minDepth(root.right), minDepth(root.left));  
   }

答案 4 :(得分:1)

JavaScript解决方案

给出以下二叉树结构:

function BT(value, left = null, right = null) {
  this.value = value;
  this.left = left;
  this.right = right;
}

找到最小深度的方法可能是这样的:

BT.prototype.getMinDepth = function() {
  if (!this.value) {
    return 0;
  }
  if (!this.left && !this.right) {
    return 1;
  }
  if (this.left && this.right) {
    return Math.min(this.left.getMinDepth(), this.right.getMinDepth()) + 1;
  }
  if (this.left) {
    return this.left.getMinDepth() + 1;
  }
  if (this.right) {
    return this.right.getMinDepth() + 1;
  }
}

上述解决方案遍历所有树节点的时间复杂度为O(n)。

更好的运行时解决方案将使用广度遍历方法,该方法在到达第一个叶节点时结束:

BT.prototype.getMinDepth = function(depth = 0) {
  if (!this.value) {
    return depth;
  }

  depth++;

  if (!this.left || !this.right) {
    return depth;
  }

  return Math.min(this.left.getMinDepth(depth), this.right.getMinDepth(depth));
}

答案 5 :(得分:0)

给定路径的深度是沿着该路径从根节点到叶节点的节点数。最小值是从根到LEAF节点的节点数最少的路径。在这种情况下,唯一的叶节点是2.(叶节点被定义为没有子节点的节点)因此,唯一的深度和最小深度是2.

Java中的示例代码:

public class Solution {
    public int minDepth(TreeNode root) {
        if (root==null) return 0;
        if ((root.left==null) || (root.right==null)) {
            return 1+Math.max(minDepth(root.left),minDepth(root.right));
        }
        return 1+Math.min(minDepth(root.left),minDepth(root.right));
    }
}

答案 6 :(得分:0)

根节点的深度为0,因此给定树的深度为1,参考下面的递归和迭代解决方案,找到二叉树的最小部分。

递归解决方案:

public static int findMinDepth(BTNode root) {
    if (root == null || (root.getLeft() == null && root.getRight() == null)) {
        return 0;
    }
    int ldepth = findMinDepth(root.getLeft());
    int rdepth = findMinDepth(root.getRight());

    return (Math.min(rdepth + 1, ldepth + 1));
}

迭代解决方案:

public static int minDepth(BTNode root) {
    int minDepth = Integer.MAX_VALUE;
    Stack<BTNode> nodes = new Stack<>();
    Stack<BTNode> path = new Stack<>();
    if (root == null) {
        return -1;
    }
    nodes.push(root);
    while (!nodes.empty()) {
        BTNode node = nodes.peek();
        if (!path.empty() && node == path.peek()) {
            if (node.getLeft() == null && node.getRight() == null && path.size() <= minDepth) {
                minDepth = path.size() - 1;
            }
            path.pop();
            nodes.pop();
        } else {
            path.push(node);
            if (node.getRight() != null) {
                nodes.push(node.getRight());
            }
            if (node.getLeft() != null) {
                nodes.push(node.getLeft());
            }
        }
    }
    return minDepth;
}

答案 7 :(得分:0)

正如其他人所说,解决方案应该是2 ......但是它是语义的,如果您对深度的定义不同,您可以简单地取结果并减去1。

这是一个迭代答案(在C#中)(Rajesh Surana回答是一个很好的回答):

public static int FindMinDepth<T>(BinarySearchTree<T> tree) where T : IComparable<T>
{
    var current = tree._root;

    int level = 0;
    Queue<BSTNode<T>> q = new Queue<BSTNode<T>>();

    if (current != null)
        q.Enqueue(current);

    while (q.Count > 0)
    {
        level++;

        Queue<BSTNode<T>> nq = new Queue<BSTNode<T>>();
        foreach (var element in q)
        {
            if (element.Left == null && element.Right == null)
                return level;

            if (element.Left != null) nq.Enqueue(element.Left);
            if (element.Right != null) nq.Enqueue(element.Right);
        }

        q = nq;                
    }

    return 0;
    //throw new Exception("Min Depth not found!");
}

答案 8 :(得分:0)

最小深度是左子树和右子树的最小深度。

public static int maxDepth(TreeNode root) {
    if(root == null) {
        return 0;
    }

    return getDepth(root);
}

private static int getDepth(TreeNode a) {
    if(a.left == null & a.right == null) {
        return 1;
    }
    int leftDepth = 0;
    int rightDepth = 0;
    if(a.left != null) {
        leftDepth = getDepth(a.left);
    }
    if(a.right != null) {
        rightDepth = getDepth(a.right);
    }
    return (Math.min(leftDepth, rightDepth)+1);
}

答案 9 :(得分:-1)

二叉树的minDepth表示从根节点到叶节点的最短距离。虽然你的二叉树的minDepth是1还是2是有争议的,这取决于你是否想要到空节点的最短距离,在这种情况下答案是1或者到一个空节点的最短距离,它的兄弟节点也是null节点,在这种情况下,二叉树{1,2}的答案是2.通常,前者被询问,并且遵循Cracking the Coding Interview中提到的算法,我们有解决方案

int minDepth(TreeNode root) {
    if (root == null) { return 0;}
        return 1 + Math.min(minDepth(root.left), minDepth(root.right));
}
相关问题