当前位置:网站首页>Yyds dry inventory solution sword finger offer: print the binary tree into multiple lines

Yyds dry inventory solution sword finger offer: print the binary tree into multiple lines

2022-06-23 00:40:00 51CTO

1. sketch :

describe

Given the number of nodes is n Binary tree , It is required to print the binary tree layer by layer from top to bottom val value , The nodes of the same layer output from left to right , Output one line per layer , Store the output results in a two-dimensional array and return . for example :

The given binary tree is {1,2,3,#,#,4,5}

#yyds Dry inventory # Solve the sword finger offer: Print a binary tree into multiple lines _ Binary tree

The result of traversal of the binary tree is [[1],[2,3],[4,5]]

Data range : The number of nodes in a binary tree  #yyds Dry inventory # Solve the sword finger offer: Print a binary tree into multiple lines _ data _02,#yyds Dry inventory # Solve the sword finger offer: Print a binary tree into multiple lines _ data _03 requirement : Spatial complexity  #yyds Dry inventory # Solve the sword finger offer: Print a binary tree into multiple lines _ data _04, Time complexity  #yyds Dry inventory # Solve the sword finger offer: Print a binary tree into multiple lines _ data _04

Input description :

Given the root node of a binary tree

Example 1

Input :

      
      
{1,2,3,#,#,4,5}
  • 1.

Return value :

      
      
[[1],[2,3],[4,5]]
  • 1.

Example 2

Input :

      
      
{8,6,10,5,7,9,11}
  • 1.

Return value :

      
      
[[8],[6,10],[5,7,9,11]]
  • 1.

Example 3

Input :

      
      
{1,2,3,4,5}
  • 1.

Return value :

      
      
[[1],[2,3],[4,5]]
  • 1.

Example 4

Input :

      
      
{}
  • 1.

Return value :

      
      
[]
  • 1.

2. Code implementation :

      
      
import java.util.*;
public class Solution {
// Level traversal
public ArrayList<ArrayList<Integer> > Print(TreeNode pRoot) {
TreeNode head = pRoot;
ArrayList<ArrayList<Integer> > res = new ArrayList<ArrayList<Integer> >();
if(head == null)
// If it's empty , It returns an empty array directly
return res;
// Queue storage , Perform hierarchical traversal
Queue<TreeNode> temp = new LinkedList<TreeNode>();
temp.offer(head);
TreeNode p;
while(!temp.isEmpty()){
// Record a row of a binary tree
ArrayList<Integer> row = new ArrayList<Integer>();
int n = temp.size();
// Because the first entry is the root node , Therefore, the number of nodes in each layer , The size of the queue is
for(int i = 0; i < n; i++){
p = temp.poll();
row.add(p.val);
// If left and right children exist , Then save the left and right children as the next level
if(p.left != null)
temp.offer(p.left);
if(p.right != null)
temp.offer(p.right);
}
res.add(row);
}
return res;
}

}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
原网站

版权声明
本文为[51CTO]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206222133320020.html