zoukankan      html  css  js  c++  java
  • 按层打印二叉树,之字形打印二叉树

    题目描述

    从上到下按层打印二叉树,同一层结点从左至右输出。每一层输出一行。

    输出:

    二维列表:[[1,2],[4,5]]

    思路:

    使用两个列表分别存放当前层节点,下一层节点
     1 # -*- coding:utf-8 -*-
     2 # class TreeNode:
     3 #     def __init__(self, x):
     4 #         self.val = x
     5 #         self.left = None
     6 #         self.right = None
     7 class Solution:
     8     # 返回二维列表[[1,2],[4,5]]
     9     def Print(self, pRoot):
    10         # write code here
    11         result = []
    12         cur_tmp = []
    13         if pRoot==None:
    14             return result
    15         cur_tmp.append(pRoot)
    16         result.append([t.val for t in cur_tmp]) 
    17         
    18         while cur_tmp:
    19             next_tmp= []
    20             for i in cur_tmp:
    21                 if i.left:
    22                     next_tmp.append(i.left)
    23                 if i.right:
    24                     next_tmp.append(i.right)
    25             if next_tmp: # 最后一层的叶子节点,next_tmp是空的
    26                 result.append([t.val for t in next_tmp]) 
    27             cur_tmp = next_tmp
    28             
    29         return result

     之字形打印二叉树

    题目描述

    请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推。

    思路

    同上面的思路,多设置了一个标志变量flag

    note:

    1.输出是二维列表形式

    2.在设置标志变量后,注意紫色代码部分,不然会重复打印

    3.下面两行代码任选一种

    result.append([next_line[i].val for i in range(len(next_line)-1,-1,-1)])    
    result.append([i.val for i in reversed(next_line)])  
    如果要倒序遍历访问序列中的元素,可以对该序列使用reversed() 函数,reversed函数会生成一份倒序列表的拷贝,但是不会改变原列表。
    第一种方式比第二种好,因为不需要更多的内存开销来存放reversed(list)副本
     1 # -*- coding:utf-8 -*-
     2 # class TreeNode:
     3 #     def __init__(self, x):
     4 #         self.val = x
     5 #         self.left = None
     6 #         self.right = None
     7 class Solution:
     8     def Print(self, pRoot):
     9         # write code here
    10         result = []
    11         cur_line = []
    12         next_line = []
    13         if pRoot==None:
    14             return result
    15         cur_line.append(pRoot)
    16         result.append([i.val for i in cur_line])
    17         flag = True
    18         while len(cur_line) != 0:
    19             for i in cur_line:
    20                 if i.left:
    21                     next_line.append(i.left)
    22                 if i.right:
    23                     next_line.append(i.right)
    24             if flag and next_line:# 偶数层,逆着打印
    25                 #result.append([next_line[i].val for i in range(len(next_line)-1,-1,-1)])    
    26                 result.append([i.val for i in reversed(next_line)])    
    27                 flag = False
    28             elif flag==False and next_line:
    29                 result.append([i.val for i in next_line])
    30                 flag = True
    31             cur_line,next_line = next_line,[]
    32         return result
    33                 
    34             
  • 相关阅读:
    Codeforces 703D Mishka and Interesting sum 离线+树状数组
    Codeforces 701E Connecting Universities 贪心
    Codeforces 680D Bear and Tower of Cubes 贪心 DFS
    Codeforces 677D Vanya and Treasure 暴力+BFS
    Codeforces 659F Polycarp and Hay 并查集
    Codeforces 676E The Last Fight Between Human and AI 规律
    hadoop 2.6.0 分布式 + Spark 1.1.0 集群环境
    Pcap 数据报解析
    Codeforces 667D World Tour 最短路
    .虚
  • 原文地址:https://www.cnblogs.com/shuangcao/p/13368616.html
Copyright © 2011-2022 走看看