• Welcome to the world's largest Chinese hacker forum

    Welcome to the world's largest Chinese hacker forum, our forum registration is open! You can now register for technical communication with us, this is a free and open to the world of the BBS, we founded the purpose for the study of network security, please don't release business of black/grey, or on the BBS posts, to seek help hacker if violations, we will permanently frozen your IP and account, thank you for your cooperation. Hacker attack and defense cracking or network Security

    business please click here: Creation Security  From CNHACKTEAM

Recommended Posts

429. N 叉树的层序遍历

给定一个普通叉树,返回其节点值的层序遍历。(即从左到右,逐层遍历)。

树的序列化输入是用层序遍历,每组子节点都由空值分隔(参见示例)。

示例1:

ewpwasx24li3641.png

输入:root=[1,null,3,2,4,null,5,6]

输出:[[1],[3,2,4],[5,6]]

示例2:

tc4ex33tqye3642.png

输入:root=[1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]

输出:[[1],[2,3,4,5],[6,7,8,9,10],[11,12,13],[14]]

提示:

树的高度不会超过1000

树的节点总数在[0,10^4]之间

1 /*

2 //节点的定义。

3类节点{

4 public:

5 int val

6矢量节点*子节点;

8节点(){}

9

10节点(int _val) {

11 val=_ val

12 }

13

14个节点(int _val,vectorNode* _children) {

15 val=_ val

16个孩子=_个孩子;

17 }

18 };

19 */

20

21类解决方案{

22 public:

23矢量矢量级别顺序(节点*根){

24矢量矢量美国国家标准(American National Standards的缩写)

25 if (root==nullptr) {

26回安;

27 }

28队列节点* q;

29 q.push(根);

30 while(!q.empty()) {

31 int size=q . size();

32矢量向量误差修正

33 for(int I=0;我尺寸;i ) {

34 Node * tmp=q . front();

35 q . pop();

36 if (tmp==nullptr) {

37继续;

38 }

39 vec。push _ back(tmp-val);

40(自动节点: tmp-子节点){

41如果(节点!=nullptr) {

42 q.push(节点);

43 }

44 }

45 }

46 ans。push _ back(vec);

47 }

48回安;

49 }

50 };

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now