[ツリー] B016_は、バイナリツリーを検証する(互いに素セット| BFS / DFS)

一つ、タイトル説明

二叉树上有 n 个节点,按从 0 到 n - 1 编号,其中节点 i 的两个子节点分别是 leftChild[i] 和 rightChild[i]。

只有 所有 节点能够形成且 只 形成 一颗 有效的二叉树时,返回 true;否则返回 false。

如果节点 i 没有左子节点,那么 leftChild[i] 就等于 -1。右子节点也符合该规则。

注意:节点没有值,本问题中仅仅使用节点编号。

第二に、問題解決

方法a:互いに素セット

バイナリ自然:

  • ルートノードは0であります
  • 一定の他のノードは1です。

上記の特性によると、私たちは、次のコードを書くことができます*が、それは一方的な答えです。0は、必ずしもルートノードではありません。

public boolean validateBinaryTreeNodes(int n, int[] leftChild, int[] rightChild) {
  int[] inDegree = new int[n];
  for (int i = 0; i < n; i++) {
    if (leftChild[i] >= 0)
        inDegree[leftChild[i]]++;
     if (rightChild[i] >= 0)
        inDegree[rightChild[i]]++;
  }

  if (inDegree[0] != 0)
      return false;
  
  for (int i = 1; i < n; i++)
    if (inDegree[i] != 1)
      return false;
  return true;
}

*我々コードロジック次のように読み取る:ノードの次数は0であるべきで、ノード1が存在しないよりも大きいです。

public boolean validateBinaryTreeNodes(int n, int[] leftChild, int[] rightChild) {
  int[] inDegree = new int[n];
  for (int i = 0; i < n; i++) {
    if (leftChild[i] != -1)
        inDegree[leftChild[i]]++;
     if (rightChild[i] != -1)
        inDegree[rightChild[i]]++;
  }
  int in0 = 0;
  int inOther = 0;
  for (int i : inDegree) {
      if (i == 0) in0++;
      if (i >= 2) inOther++;
  }
  return in0 == 1 && inOther == 0;
}

:ここでのポイントは、見過ごされている*ツリーを満たす唯一のノード0度の環の存在、およびノードの不在が1より大きい場合。

したがって、簡単な統計ノードの実装の度合いは意味がありません、我々はリングを判断する必要があります。だからここだけ行うには互いに素-セットを持ちます。

コアロジックは2つあり:

  • 2つのノードが子ノードの父が最初に所有することができる場所、通信していません。
  • 任意の2つのノード、すなわち、リングが存在しない、切断されます。
public boolean validateBinaryTreeNodes(int n, int[] leftChild, int[] rightChild) {
  UF uf = new UF(n);
  for (int i = 0; i < n; i++) {
    if (leftChild[i] != -1) {
      if (uf.find(leftChild[i]) != leftChild[i] || uf.isConn(leftChild[i], i))
          return false;
      uf.union(i, leftChild[i]);
    }
    if (rightChild[i] != -1) {
      if (uf.find(rightChild[i]) != rightChild[i] || uf.isConn(rightChild[i], i))
          return false;
      uf.union(i, rightChild[i]);
    }
  }
  return uf.count == 1;
}
class UF {
	int[] parent;
	int[] size;
    int count;
    public UF(int N) {
	    count = N;
	    parent = new int[N];
	    size = new int[N];
	    for(int i = 0; i < N; i++) {
		    parent[i] = i;
		    size[i] = 1;
	    }
	}
    public boolean isConn(int p, int q) {
        return find(p) == find(q);
    }
	public int find(int p) {
	    while (p != parent[p]) {
	        p = parent[p];
	    }
	    return p;
	}
	public void union(int p, int q) {
	    int pRootID = find(p);
	    int qRootID = find(q);
	
	    if(pRootID == qRootID)  return;
	
	    if(size[pRootID] > size[qRootID]) {
	        parent[qRootID] = pRootID;
	        size[pRootID] += size[qRootID];
	    }else {
	        parent[pRootID] = qRootID;
	        size[qRootID] += size[pRootID];
	    }
	    count--;  // 连通分量减一
	}
}

複雑性分析

  • 時間計算: ザ・ ()
  • 宇宙の複雑さ: ザ・ ()

方法2:DFSやBFSジャッジメントリング


複雑性分析

  • 時間計算: ザ・ ()
  • 宇宙の複雑さ: ザ・ ()
公開された495元の記事 ウォンの賞賛105 ・は 30000 +を見て

おすすめ

転載: blog.csdn.net/qq_43539599/article/details/104867560