二叉树每一层的最大值
给定一棵二叉树,找出每一层的最大值。
例子 :
Input :
1
/ \
2 3
Output : 1 3
Input :
4
/ \
9 2
/ \ \
3 5 7
Output : 4 9 7
方法:这个想法是以预先顺序的方式递归遍历树。根被认为是在零级。遍历时,跟踪元素的级别,如果其当前级别不等于列表中存在的元素数量,则更新列表中该级别的最大元素。
下面是在二叉树的每一层上找到最大值的实现。
C++
// C++ program to find largest
// value on each level of binary tree.
#include
using namespace std;
/* A binary tree node has data,
pointer to left child and a
pointer to right child */
struct Node {
int val;
struct Node *left, *right;
};
/* Recursive function to find
the largest value on each level */
void helper(vector& res, Node* root, int d)
{
if (!root)
return;
// Expand list size
if (d == res.size())
res.push_back(root->val);
else
// to ensure largest value
// on level is being stored
res[d] = max(res[d], root->val);
// Recursively traverse left and
// right subtrees in order to find
// out the largest value on each level
helper(res, root->left, d + 1);
helper(res, root->right, d + 1);
}
// function to find largest values
vector largestValues(Node* root)
{
vector res;
helper(res, root, 0);
return res;
}
/* Helper function that allocates a
new node with the given data and
NULL left and right pointers. */
Node* newNode(int data)
{
Node* temp = new Node;
temp->val = data;
temp->left = temp->right = NULL;
return temp;
}
// Driver code
int main()
{
/* Let us construct a Binary Tree
4
/ \
9 2
/ \ \
3 5 7 */
Node* root = NULL;
root = newNode(4);
root->left = newNode(9);
root->right = newNode(2);
root->left->left = newNode(3);
root->left->right = newNode(5);
root->right->right = newNode(7);
vector res = largestValues(root);
for (int i = 0; i < res.size(); i++)
cout << res[i] << " ";
return 0;
}
Java
// Java program to find largest
// value on each level of binary tree.
import java.util.*;
class GFG
{
/* A binary tree node has data,
pointer to left child and a
pointer to right child */
static class Node
{
int val;
Node left, right;
};
/* Recursive function to find
the largest value on each level */
static void helper(Vector res, Node root, int d)
{
if (root == null)
return;
// Expand list size
if (d == res.size())
res.add(root.val);
else
// to ensure largest value
// on level is being stored
res.set(d, Math.max(res.get(d), root.val));
// Recursively traverse left and
// right subtrees in order to find
// out the largest value on each level
helper(res, root.left, d + 1);
helper(res, root.right, d + 1);
}
// function to find largest values
static Vector largestValues(Node root)
{
Vector res = new Vector<>();
helper(res, root, 0);
return res;
}
/* Helper function that allocates a
new node with the given data and
NULL left and right pointers. */
static Node newNode(int data)
{
Node temp = new Node();
temp.val = data;
temp.left = temp.right = null;
return temp;
}
// Driver code
public static void main(String[] args)
{
/* Let us construct a Binary Tree
4
/ \
9 2
/ \ \
3 5 7 */
Node root = null;
root = newNode(4);
root.left = newNode(9);
root.right = newNode(2);
root.left.left = newNode(3);
root.left.right = newNode(5);
root.right.right = newNode(7);
Vector res = largestValues(root);
for (int i = 0; i < res.size(); i++)
System.out.print(res.get(i)+" ");
}
}
/* This code is contributed by PrinciRaj1992 */
Python3
# Python program to find largest value
# on each level of binary tree.
""" Recursive function to find
the largest value on each level """
def helper(res, root, d):
if ( not root):
return
# Expand list size
if (d == len(res)):
res.append(root.val)
else:
# to ensure largest value
# on level is being stored
res[d] = max(res[d], root.val)
# Recursively traverse left and
# right subtrees in order to find
# out the largest value on each level
helper(res, root.left, d + 1)
helper(res, root.right, d + 1)
# function to find largest values
def largestValues(root):
res = []
helper(res, root, 0)
return res
# Helper function that allocates a new
# node with the given data and None left
# and right pointers.
class newNode:
# Constructor to create a new node
def __init__(self, data):
self.val = data
self.left = None
self.right = None
# Driver Code
if __name__ == '__main__':
""" Let us construct the following Tree
4
/ \
9 2
/ \ \
3 5 7 """
root = newNode(4)
root.left = newNode(9)
root.right = newNode(2)
root.left.left = newNode(3)
root.left.right = newNode(5)
root.right.right = newNode(7)
print(*largestValues(root))
# This code is contributed
# Shubham Singh(SHUBHAMSINGH10)
C#
// C# program to find largest
// value on each level of binary tree.
using System;
using System.Collections.Generic;
class GFG
{
/* A binary tree node has data,
pointer to left child and a
pointer to right child */
public class Node
{
public int val;
public Node left, right;
};
/* Recursive function to find
the largest value on each level */
static void helper(List res,
Node root, int d)
{
if (root == null)
return;
// Expand list size
if (d == res.Count)
res.Add(root.val);
else
// to ensure largest value
// on level is being stored
res[d] = Math.Max(res[d], root.val);
// Recursively traverse left and
// right subtrees in order to find
// out the largest value on each level
helper(res, root.left, d + 1);
helper(res, root.right, d + 1);
}
// function to find largest values
static List largestValues(Node root)
{
List res = new List();
helper(res, root, 0);
return res;
}
/* Helper function that allocates a
new node with the given data and
NULL left and right pointers. */
static Node newNode(int data)
{
Node temp = new Node();
temp.val = data;
temp.left = temp.right = null;
return temp;
}
// Driver code
public static void Main(String[] args)
{
/* Let us construct a Binary Tree
4
/ \
9 2
/ \ \
3 5 7 */
Node root = null;
root = newNode(4);
root.left = newNode(9);
root.right = newNode(2);
root.left.left = newNode(3);
root.left.right = newNode(5);
root.right.right = newNode(7);
List res = largestValues(root);
for (int i = 0; i < res.Count; i++)
Console.Write(res[i] + " ");
}
}
// This code is contributed by 29AjayKumar
Javascript
输出:
4 9 7
二叉树每一层的最大值 | Set-2(迭代方法)
复杂性分析:
- 时间复杂度: O(n),其中 n 是二叉树的节点数。
- 辅助空间: O(n),在最坏的情况下,二叉树的深度为 n。