从上到下打印二叉树

从上往下打印出二叉树的每个节点,同层节点从左至右打印。

 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,3]
 9     def __init__(self):
10         self.list = []
11         
12     def levelOrder(self,level):
13         count = len(level)
14         while count > 0:
15             root = level.pop(0)
16             self.list.append(root.val)
17             if root.left != None:
18                 level.append(root.left)
19             if root.right != None:
20                 level.append(root.right)
21             count -= 1
22         if len(level) > 0:
23             self.levelOrder(level)
24         
25         
26     def PrintFromTopToBottom(self, root):
27         if root == None:
28             return self.list
29         else:
30             level = []
31             level.append(root)
32             self.levelOrder(level)
33             return self.list
34         # write code here

 

Java版代码,leetcode地址

 1 public class Solution {
 2 
 3         ArrayList<Integer> list = new ArrayList<Integer>();
 4         Queue<TreeNode> queue = new LinkedList<TreeNode>();
 5 
 6         public void preOrder() {
 7             ArrayList<TreeNode> tempArrayList = new ArrayList<TreeNode>();
 8             while (!queue.isEmpty()) {
 9                 TreeNode tNode = queue.poll();
10                 list.add(tNode.val);
11                 if (tNode.left != null) {
12                     tempArrayList.add(tNode.left);
13                 }
14                 if (tNode.right != null) {
15                     tempArrayList.add(tNode.right);
16                 }
17             }
18             if (!tempArrayList.isEmpty()) {
19                 queue.addAll(tempArrayList);
20                 preOrder();
21             }
22         }
23 
24         public int[] levelOrder(TreeNode root) {
25             if(root == null){
26                 return new int[0];
27             }
28             queue.add(root);
29             preOrder();
30             return list.stream().mapToInt(Integer::valueOf).toArray();
31         }
32     }

 

 

打印成多行

 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 __init__(self):
10         self.list = []
11         self.result = []
12         
13     def levelOrder(self,level):
14         count = len(level)
15         while count > 0:
16             root = level.pop(0)
17             self.list.append(root.val)
18             if root.left != None:
19                 level.append(root.left)
20             if root.right != None:
21                 level.append(root.right)
22             count -= 1
23         if len(self.list) > 0:
24             self.result.append(self.list[:])
25             self.list = []
26         if len(level) > 0:
27             self.levelOrder(level)
28             
29     def Print(self, pRoot):
30         if pRoot == None:
31             return self.list
32         else:
33             level = []
34             level.append(pRoot)
35             self.levelOrder(level)
36             return self.result
37         # write code here

 

Java版代码,leetcode地址

 1 class Solution {
 2         List<List<Integer>> list = new ArrayList<List<Integer>>();
 3         Queue<TreeNode> queue = new LinkedList<TreeNode>();
 4                     
 5         public void preOrder() {
 6             List<Integer> leveList = new ArrayList<Integer>();
 7             List<TreeNode> tempList = new ArrayList<TreeNode>();
 8             while(!queue.isEmpty()) {
 9                 TreeNode curNode = queue.poll();
10                 leveList.add(curNode.val);
11                 if(curNode.left!=null) {
12                     tempList.add(curNode.left);
13                 }
14                 if(curNode.right!=null) {
15                     tempList.add(curNode.right);
16                 }
17             }
18             if(!leveList.isEmpty()) {
19                 list.add(leveList);
20             }
21             if(!tempList.isEmpty()) {
22                 queue.addAll(tempList);
23                 preOrder();
24             }            
25         }
26         
27         public List<List<Integer>> levelOrder(TreeNode root) {
28             if(root==null) {
29                 return new ArrayList<List<Integer>>();
30             }
31             queue.add(root);
32             preOrder();
33             return list;
34         }
35     }

 

 

打印成之字型

 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 __init__(self):
10         self.list = []
11         self.result = []
12         self.rev = -1
13         
14     def levelOrder(self,level):
15         count = len(level)
16         while count > 0:
17             root = level.pop(0)
18             self.list.append(root.val)
19             if root.left != None:
20                 level.append(root.left)
21             if root.right != None:
22                 level.append(root.right)
23             count -= 1
24         if len(self.list) > 0:
25             self.rev *= -1
26             self.result.append(self.list[::self.rev])
27             self.list = []
28         if len(level) > 0:
29             self.levelOrder(level)
30     def Print(self, pRoot):
31         if pRoot == None:
32             return self.list
33         else:
34             level = []
35             level.append(pRoot)
36             self.levelOrder(level)
37             return self.result
38         # write code here

 

posted on 2019-06-13 07:10  Sempron2800+  阅读(147)  评论(0编辑  收藏  举报