具有空值的二叉树的最大宽度 |设置 2
先决条件:空值二叉树的最大宽度 |设置 1
给定一棵由N个节点组成的二叉树,任务是在不使用递归的情况下找到给定树的最大宽度,其中最大宽度定义为给定树的每一层的所有宽度中的最大值。
注意:任何级别的树的宽度定义为该级别的两个极端节点之间的节点数,包括其间的 NULL 节点。
例子:
Input:
1
/ \
2 3
/ \ \
4 5 8
/ \
6 7
Output: 4
Exaplanation:
1 // width = 1
/ \
2 3 // width = 2 – 1 + 1= 2
/ \ \
4 5 8 // width = 6 – 3 + 1 = 4
/ \
6 7 // width = 8 – 7 + 1 = 2
So, the answer is 4
Input:
1
/
2
/
3
Output: 1
方法:在这种方法中,主要思想是使用关卡顺序遍历并根据其父节点为所有节点提供一个id,这将有助于找到特定关卡的宽度。 Ids 按以下特定顺序分布:
[node, id: i]
/ \
[left child, id: (i * 2 +1)] [right child, id: (i * 2 + 2)]
现在可以使用公式计算每个级别的宽度
Width of level i = (id of first node at level i) – (id of last node at level i) +1
说明:例如,使用此处提供的第一个示例。
{1,0} // width = 1
/ \
{2,1} {3,2} // width = 2 – 1 + 1= 2
/ \ \
{4,3} {5,4} {8,6} // width = 6 – 3 + 1 = 4
/ \
{6,7} {7,8} // width = 8 – 7 + 1 = 2
So, the answer is 4
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Tree Node structure
struct Node {
int data;
Node *left, *right;
// Constructor
Node(int item)
{
data = item;
left = right = NULL;
}
};
// Function to find the width
// of the given tree
int getMaxWidth(Node* root)
{
if (root == NULL) {
return 0;
}
queue > q;
q.push({ root, 0 });
int maxWidth = 1;
while (!q.empty()) {
int size = q.size();
int first, last;
for (int i = 0; i < size; i++) {
Node* temp = q.front().first;
int id = q.front().second;
q.pop();
// First Node
if (i == 0) {
first = id;
}
// Last Node
if (i == size - 1) {
last = id;
};
if (temp->left)
q.push({ temp->left,
id * 2 + 1 });
if (temp->right)
q.push({ temp->right,
id * 2 + 2 });
}
maxWidth = max(maxWidth,
last - first + 1);
}
return maxWidth;
}
// Driver Code
int main()
{
struct Node* root = new Node(1);
root->left = new Node(2);
root->right = new Node(3);
root->left->left = new Node(4);
root->left->right = new Node(5);
root->right->right = new Node(8);
root->right->right->left = new Node(6);
root->right->right->right = new Node(7);
// Function Call
cout << getMaxWidth(root);
return 0;
}
Java
// Java program for the above approach
import java.util.LinkedList;
import java.util.Queue;
class GFG {
// Tree Node structure
public static class Node {
int data;
Node left;
Node right;
// Constructor
public Node(int item) {
this.data = item;
this.left = this.right = null;
}
};
public static class Pair {
Node first;
int second;
public Pair(Node n, int i) {
this.first = n;
this.second = i;
}
}
// Function to find the width
// of the given tree
static int getMaxWidth(Node root) {
if (root == null) {
return 0;
}
Queue q = new LinkedList();
q.add(new Pair(root, 0));
int maxWidth = 1;
while (!q.isEmpty()) {
int size = q.size();
int first = 0, last = 0;
for (int i = 0; i < size; i++) {
Node temp = q.peek().first;
int id = q.peek().second;
q.remove();
// First Node
if (i == 0) {
first = id;
}
// Last Node
if (i == size - 1) {
last = id;
}
;
if (temp.left != null)
q.add(new Pair(temp.left, id * 2 + 1));
if (temp.right != null)
q.add(new Pair(temp.right, id * 2 + 2));
}
maxWidth = Math.max(maxWidth, last - first + 1);
}
return maxWidth;
}
// Driver Code
public static void main(String args[]) {
Node root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
root.right.right = new Node(8);
root.right.right.left = new Node(6);
root.right.right.right = new Node(7);
// Function Call
System.out.println(getMaxWidth(root));
}
}
// This code is contributed by saurabh_jaiswal.
Python3
# Python program for the above approach
# Tree Node structure
class Node:
# Constructor
def __init__(self, item):
self.data = item
self.left = self.right = None
# Function to find the width
# of the given tree
def getMaxWidth(root):
if (root == None):
return 0
q = []
q.append([root, 0])
maxWidth = 1
while (len(q)):
size = len(q)
first = None
last = None
for i in range(size):
temp = q[0][0]
id = q[0][1]
q.pop(0)
# First Node
if (i == 0):
first = id
# Last Node
if (i == size - 1):
last = id
if (temp.left):
q.append([temp.left, id * 2 + 1])
if (temp.right):
q.append([temp.right, id * 2 + 2])
maxWidth = max(maxWidth, last - first + 1)
return maxWidth
# Driver Code
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
root.right.right = Node(8)
root.right.right.left = Node(6)
root.right.right.right = Node(7)
# Function Call
print(getMaxWidth(root))
# This code is contributed by Saurabh jaiswal
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
public class GFG {
// Tree Node structure
public class Node {
public int data;
public Node left;
public Node right;
// Constructor
public Node(int item) {
this.data = item;
this.left = this.right = null;
}
};
public class Pair {
public Node first;
public int second;
public Pair(Node n, int i) {
this.first = n;
this.second = i;
}
}
// Function to find the width
// of the given tree
static int getMaxWidth(Node root) {
if (root == null) {
return 0;
}
Queue q = new Queue();
q.Enqueue(new Pair(root, 0));
int maxWidth = 1;
while (q.Count!=0) {
int size = q.Count;
int first = 0, last = 0;
for (int i = 0; i < size; i++) {
Node temp = q.Peek().first;
int id = q.Peek().second;
q.Dequeue();
// First Node
if (i == 0) {
first = id;
}
// Last Node
if (i == size - 1) {
last = id;
}
;
if (temp.left != null)
q.Enqueue(new Pair(temp.left, id * 2 + 1));
if (temp.right != null)
q.Enqueue(new Pair(temp.right, id * 2 + 2));
}
maxWidth = Math.Max(maxWidth, last - first + 1);
}
return maxWidth;
}
// Driver Code
public static void Main(String []args) {
Node root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
root.right.right = new Node(8);
root.right.right.left = new Node(6);
root.right.right.right = new Node(7);
// Function Call
Console.WriteLine(getMaxWidth(root));
}
}
// This code is contributed by shikhasingrajput
Javascript
4
时间复杂度: O(N)
辅助空间: O(N)