日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

國(guó)內(nèi)最全I(xiàn)T社區(qū)平臺(tái) 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當(dāng)前位置:首頁(yè) > php開源 > php教程 > LeetCode Binary Tree Zigzag Level Order Traversal

LeetCode Binary Tree Zigzag Level Order Traversal

來源:程序員人生   發(fā)布時(shí)間:2015-05-14 09:37:38 閱讀次數(shù):2536次

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}".

題意:層次遍歷1顆樹,奇數(shù)層的時(shí)候翻轉(zhuǎn)。

思路:還是利用隊(duì)列層次遍歷,多1個(gè)判斷是不是翻轉(zhuǎn)的標(biāo)記就好了,還有就是java是援用傳遞的,所以每次都要重新聲明1個(gè)對(duì)象,不然ans里面都是指向同1塊內(nèi)存。

/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public List<List<Integer>> zigzagLevelOrder(TreeNode root) { List<List<Integer>> ans = new ArrayList<List<Integer>>(); if (root == null) return ans; Queue<TreeNode> queue = new LinkedList<TreeNode>(); queue.add(root); boolean reverse = false; while (!queue.isEmpty()) { List<Integer> tmp = new ArrayList<Integer>(); int num = queue.size(); for (int i = 0; i < num; i++) { TreeNode t = queue.poll(); tmp.add(t.val); if (t.left != null) queue.add(t.left); if (t.right != null) queue.add(t.right); } if (reverse) { Collections.reverse(tmp); reverse = false; } else reverse = true; ans.add(tmp); } return ans; } }


生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關(guān)閉
程序員人生
主站蜘蛛池模板: 亚洲精品视频观看 | julia中文字幕久久一区二区 | 欧美一区1区三区3区公司 | 99在线免费视频 | 99日韩精品| 亚洲在线一区二区 | av网址在线看| 一区二区三区四区不卡视频 | 成人免费视屏 | 久久精品夜夜夜夜夜久久 | 黄污污网站| 日本精品国产 | 成人在线免费观看 | 久久这里都是精品 | 亚洲视频中文字幕 | 国产精品成人在线 | 亚洲免费一区 | 黄色一级视频 | 国产精品一区二区不卡 | 欧美日韩中文字幕在线 | 99毛片| 秋霞毛片少妇激情免费 | 久久久精品国产 | 欧美日韩二区三区 | 亚洲1区| 黄色av国产 | 亚洲国产一区二区视频 | 在线视频a | 欧美日韩中文字幕在线视频 | 精品成人在线视频 | 欧美一区二 | 亚洲三区在线观看 | 精品国产乱码久久久久久影片 | 麻豆av在线播放 | 国产成人免费视频网站视频社区 | а√ 天堂 在线官网 | 激情综合五月 | 久久久久国产精品一区 | 四虎884aa成人精品最新 | 日本欧美在线 | 国产原创视频 |