【树】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]
]

思路:

首先肯定的是要对树进行层序遍历,但是相邻两层的元素遍历顺序是相反的,因此传统的非递归遍历方法用一个队列肯定是无法实现。我们用两个栈来实现,同一层元素都在同一个栈中,相邻的两层元素放在不同的栈中,比如第一层元素放在第一个栈,那么第二层就放在第二个栈,第三层再放在第一个栈......如果某一层元素是从左往右遍历的,那么这层元素的孩子节点入栈顺序就是先左孩子后右孩子,相反如果某一层元素是从右往左遍历的,入栈顺序就是先右孩子后左孩子。

/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[][]}
 */
var zigzagLevelOrder = function(root) {
    var res=[],tempres=[];
    if(root==null){
        return res;
    }
    var s1=[],s2=[];
    s1.push(root);
    var flag=true;

    while(s1.length!=0||s2.length!=0){
        var p=null;

        if(flag){
            p=s1.pop();
            tempres.push(p.val);
            if(p.left){
                s2.push(p.left);
            }
            if(p.right){
                s2.push(p.right);
            }
            if(s1.length==0){
                var temp=[];
                for(var i=0,len=tempres.length;i<len;i++){
                    temp[i]=tempres[i];
                }
                res.push(temp);
                tempres.length=0;
                flag=false;
            }
        }else{
            p=s2.pop();
            tempres.push(p.val);
            if(p.right){
                s1.push(p.right);
            }
            if(p.left){
                s1.push(p.left);
            }
            if(s2.length==0){
                var temp=[];
                for(var i=0,len=tempres.length;i<len;i++){
                    temp[i]=tempres[i];
                }
                res.push(temp);
                tempres.length=0;
                flag=true;
            }
        }
    }

    return res;
};
时间: 2024-10-14 20:48:10

【树】Binary Tree Zigzag Level Order Traversal的相关文章

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

39.2: Binary Tree Zigzag Level Order Traversal

/************************************************************************/        /*       38:      Binary Tree Zigzag Level Order Traversal                                       */        /************************************************************

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] 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

37. Binary Tree Zigzag Level Order Traversal &amp;&amp; Binary Tree Inorder 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,

Binary Tree Zigzag Level Order Traversal

原题: 题目解析:这个问题的实质是要我们按成访问二叉树的结点,并返回每层访问的结果,这里要求走Z字,其实就是一行正向一行反向. /* the kernel idea is visit a binary search tree in level and the additional work we have to label the end of one level. */ vector<vector<int> > zigzagLevelOrder(TreeNode *root) {

[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,#

[Lintcode]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). Example Given binary tree {3,9,20,#,#,1

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 @ 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