[leetcode]Binary Tree Zigzag Level Order Traversal @ Python

原题地址:http://oj.leetcode.com/problems/binary-tree-zigzag-level-order-traversal/

题意:

Given a binary tree, return the zigzag level
order
 traversal of its nodes‘ values. (ie, from left to right, then
right to left for the next level and alternate between).

For example:
Given binary tree {3,9,20,#,#,15,7},

    3
/ 9 20
/ 15 7

return its zigzag level order traversal as:

[
[3],
[20,9],
[15,7]
]

confused
what "{1,#,2,3}" means? >
read more on how binary tree is serialized on OJ.

OJ‘s Binary Tree Serialization:

The serialization of a binary tree follows a level order traversal, where ‘#‘
signifies a path terminator where no node exists below.

Here‘s an example:

   1
/ 2 3
/
4
5
The above binary tree is
serialized as "{1,2,3,#,#,4,#,#,5}".

解题思路:这道题和层序遍历那道题差不多,区别只是在于奇数层的节点要翻转过来存入数组。http://www.cnblogs.com/zuoyuan/p/3722004.html

代码:


# Definition for a  binary tree node
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None

class Solution:
# @param root, a tree node
# @return a list of lists of integers
def preorder(self, root, level, res):
if root:
if len(res) < level+1: res.append([])
if level % 2 == 0: res[level].append(root.val)
else: res[level].insert(0, root.val)
self.preorder(root.left, level+1, res)
self.preorder(root.right, level+1, res)
def zigzagLevelOrder(self, root):
res=[]
self.preorder(root, 0, res)
return res

[leetcode]Binary Tree Zigzag Level Order Traversal @
Python,布布扣,bubuko.com

[leetcode]Binary Tree Zigzag Level Order Traversal @
Python

时间: 2024-10-07 05:25:11

[leetcode]Binary Tree Zigzag Level Order Traversal @ Python的相关文章

[leetcode]Binary Tree Zigzag Level Order Traversal

Binary Tree Zigzag Level Order Traversal Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example:Given binary tree {3,9,20,#

[LeetCode] Binary Tree Zigzag Level Order Traversal(bfs)

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its zig

[LeetCode] Binary Tree Zigzag Level Order Traversal 二叉树的之字形层序遍历

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its zigz

LeetCode :: Binary Tree Zigzag Level Order Traversal [tree, BFS]

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its zig

LeetCode—Binary Tree Zigzag Level Order Traversal 二叉树的Z扫描

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its zig

leetcode 刷题之路 63 Binary Tree Zigzag Level Order Traversal

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its zig

leetcode -day24 Maximum Depth of Binary Tree &amp; Binary Tree Zigzag Level Order Traversal

1.Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. class Solution { public: int maxDepth(TreeNode *root) { inM

Leetcode 树 Binary Tree Zigzag Level Order Traversal

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Binary Tree Zigzag Level Order Traversal Total Accepted: 8678 Total Submissions: 33047My Submissions Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from le

Binary Tree ZigZag Level Order Traversal leetcode java

题目: Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its