103. Binary Tree Zigzag Level Order Traversal

二刷。

BFS,基本習慣上用Iterative的做法來做,就是QUEUE。

/*** Definition for a binary tree node.* 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>> res = new ArrayList<List<Integer>>();if(root == null) return res;Queue<TreeNode> q = new LinkedList<TreeNode>();q.add(root);boolean left = false;int cur = 1;int total = 0;TreeNode temp = null;List<Integer> tempList = new ArrayList<Integer>();while(!q.isEmpty()){temp = q.poll();cur--;tempList.add(temp.val);if(temp.left != null){q.add(temp.left);total++;}if(temp.right != null){q.add(temp.right);total++;}if(cur == 0){cur = total;total = 0;if(!left){res.add(new ArrayList<Integer>(tempList));left = !left;}else{Collections.reverse(tempList);res.add(new ArrayList<Integer>(tempList));left = !left;}tempList = new ArrayList<Integer>();}}return res;}
}

reverse一個LIST的函數是Collections.reverse(list),不是list.reverse()。。那個是StringBuilder。

看一刷說還有Recursivley的做法。

然后試試發現還真行。。

public class Solution {public List<List<Integer>> zigzagLevelOrder(TreeNode root) {List<List<Integer>> res = new ArrayList<List<Integer>>();if(root == null) return res;helper(res,root,0);for(int i = 1; i < res.size();i+=2){Collections.reverse(res.get(i));}return res;}public void helper(List<List<Integer>> res, TreeNode root, int level){if(root == null) return;List<Integer> tempList = new ArrayList<Integer>();if(level > res.size()-1) res.add(new ArrayList<Integer>());res.get(level).add(root.val);helper(res,root.left,level+1);helper(res,root.right,level+1);}
}

beat 95%..



三刷。

盡量加的時候就弄好順序,別最后排序。。

Recursion:

public class Solution {public List<List<Integer>> zigzagLevelOrder(TreeNode root) {List<List<Integer>> res = new ArrayList<>();if (root == null) return res;dfs(root, res, true, 0);return res;}public void dfs(TreeNode root, List<List<Integer>> res, boolean fromLeft, int level) {if (root == null) return;if (level == res.size()) {res.add(new ArrayList<>());}if (fromLeft) {res.get(level).add(root.val);} else {res.get(level).add(0,root.val);}dfs(root.left, res, !fromLeft, level + 1);dfs(root.right, res, !fromLeft, level + 1);}
}

Iteration:

public class Solution {public List<List<Integer>> zigzagLevelOrder(TreeNode root) {List<List<Integer>> res = new ArrayList<>();if (root == null) return res;res.add(new ArrayList<>());TreeNode temp = null;boolean fromLeft = true;int left = 1;       // how many nodes left in this levelint total = 0;int level = 0;Queue<TreeNode> q = new LinkedList<>();q.offer(root);while (!q.isEmpty()) {temp = q.poll();if (fromLeft) {res.get(level).add(temp.val);} else {res.get(level).add(0, temp.val);}if (temp.left != null) {q.offer(temp.left);total ++;}if (temp.right != null) {q.offer(temp.right);total ++;}if (-- left == 0) {left = total;total = 0;level ++;fromLeft = !fromLeft;if (!q.isEmpty()) {res.add(new ArrayList<>());}}}return res;}
}

轉載于:https://www.cnblogs.com/reboot329/p/6106052.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/371967.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/371967.shtml
英文地址,請注明出處:http://en.pswp.cn/news/371967.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

java多線程系列13 設計模式 Future 模式

Future 模式 類似于ajax請求 頁面異步的進行后臺請求 用戶無需等待請求的結果 就可以繼續瀏覽或者操作 核心就是&#xff1a;去除了主函數的等待時間&#xff0c;并使得原本需要等待的時間段可以用于處理其他業務邏輯 JDK內置實現Future模式演示一下 public class RealData im…

lodop轉到其他html頁面,Lodop實現打印功能

思路&#xff1a;1、在 html 頁面引入 LodopFuncs.js 文件&#xff0c;并用 object 標簽和 embed 標簽獲取 lodop 對象2、在 js 中獲取 html 頁面中的 object 和 embed 對象&#xff0c;并使用getLodop() 方法得到 lodop 對象3、實現打印功能&#xff0c;以下三步是必需的初始化…

完整的Web應用程序Tomcat JSF Primefaces JPA Hibernate –第3部分

Primefaces AutoComplete&#xff0c;JSF轉換器 這篇文章從第一部分和第二部分繼續。 JSF擁有Converter工具&#xff0c;可以幫助我們從用戶視圖中獲取一些數據并將其轉換為從數據庫或緩存中加載的對象。 在“ com.converter”包中&#xff0c;創建以下類&#xff1a; packa…

html5首屏加載樂山暴雨,發布前端項目時因chunk-vendors過大導致首屏加載太慢,Vue Build時chunk-vendors的優化方案...

這個優化是兩方面的&#xff0c;前端將文件打包成.gz文件&#xff0c;然后通過nginx的配置&#xff0c;讓瀏覽器直接解析.gz文件。1、compression-webpack-plugin插件打包.gz文件安裝插件npm install --save-dev compression-webpack-plugin或者yarn add compression-webpack-p…

width:100vh與min-height:calc(100vh + 51px)

vh:相對于視窗的高度&#xff0c;那么vw:則是相對于視窗的高度。 “視區”所指為瀏覽器內部的可視區域大小&#xff0c;即window.innerWidth/window.innerHeight大小&#xff0c;不包含任務欄標題欄以及底部工具欄的瀏覽器區域大小。 詳細vh的用法&#xff0c;大家可以參考http…

XML配置文件中的Spring配置文件

我的上一個博客非常簡單&#xff0c;因為它涵蓋了我從Spring 3.0.x到Spring 3.1.x的輕松升級&#xff0c;最后我提到可以將Spring模式升級到3.1&#xff0c;以利用Spring的最新功能。 在今天的博客中&#xff0c;我將介紹這些功能中最酷的功能之一&#xff1a;Spring配置文件。…

交大計算機專業怎樣,計算機專業高校實力排名,上海交大第五,清華第二,第一毫無爭議...

原標題&#xff1a;計算機專業高校實力排名&#xff0c;上海交大第五&#xff0c;清華第二&#xff0c;第一毫無爭議計算機專業在近幾年可謂是“大熱”&#xff0c;眾多考生搶破頭也想當碼農&#xff0c;背后的原因其實不難理解。互聯網時代的到來&#xff0c;計算機早已滲透到…

python_day7 綁定方法與非綁定方法

在類中定義函數如果 不加裝飾器 則默認 為對象作為綁定方法 如果增加 classmethod 是 以 類 作為綁定方法 增加 classmethod 是 非綁定方法&#xff0c;就是不將函數 綁定 ##################### class Foo: def func(self): print(self) classmethod def func…

Spring Security使用Hibernate實現自定義UserDetails

大多數時候&#xff0c;我們將需要在Web應用程序中配置自己的安全訪問角色。 這在Spring Security中很容易實現。 在本文中&#xff0c;我們將看到最簡單的方法。 首先&#xff0c;我們將在數據庫中需要以下表格&#xff1a; CREATE TABLE IF NOT EXISTS mydb.security_role (…

python之路-面向對象

編程范式 編程是 程序 員 用特定的語法數據結構算法組成的代碼來告訴計算機如何執行任務的過程 &#xff0c; 一個程序是程序員為了得到一個任務結果而編寫的一組指令的集合&#xff0c;正所謂條條大路通羅馬&#xff0c;實現一個任務的方式有很多種不同的方式&#xff0c; 對這…

西安郵電大學計算機科學與技術有專碩嗎,2020年西安郵電大學計算機學院考研擬錄取名單及排名!...

20考研復試調劑群&#xff1a;4197552812020年西安郵電大學計算機學院碩士研究生招生復試成績及綜合排名各位考生&#xff1a;現將我院2020年碩士研究生招生復試成績及綜合排名公布(最終錄取名單及新生學籍注冊均以“全國碩士研究生招生信息公開平臺”備案信息為準)&#xff0c…

用Java排序的五種有用方法

Java排序快速概述&#xff1a; 正常的列表&#xff1a; private static List VEGETABLES Arrays.asList("apple", "cocumbers", "blackberry");Collections.sort(VEGETABLES);output: apple, blackberry, cocumbers反向排序&#xff1a; pri…

[python]-數據科學庫Numpy學習

一、Numpy簡介&#xff1a; Python中用列表(list)保存一組值&#xff0c;可以用來當作數組使用&#xff0c;不過由于列表的元素可以是任何對象&#xff0c;因此列表中所保存的是對象的指針。這樣為了保存一個簡單的[1,2,3]&#xff0c;需要有3個指針和三個整數對象。對于數值運…

檢測一個點, 是否在一個半圓之內的方法

demo: http://jsbin.com/lihiwigaso 需求: 一個圓分成分部分, 鼠標滑上不同的區域顯示不同的顏色 思路: 先判斷這個點是否在圓之內, 再判斷是否在所在的三角形之內就可以了 所需要的全部源碼: <!DOCTYPE html> <html> <head><meta charset"utf-8&quo…

計算機網絡設備接地規范,網絡機房防雷接地的四種方式及靜電要求

編輯----河南新時代防雷由于計算機網絡系統的核心設備都放置在網絡機房內&#xff0c;因而網絡機房防雷接地有了較高的環境要求&#xff0c;良好的接地系統是保證機房計算機及網絡設備安全運行&#xff0c;以及工作人員人身安全的重要措施。直流地的接法通常采用網格地&#xf…

抓住尾部的StackOverFlowError

使用Java程序時可能要處理的一種更煩人的情況是StackOverFlowError&#xff0c;如果您有一個很好的可生產的測試用例&#xff0c;那么關于使用堆棧大小或設置條件斷點/某種痕跡 。 但是&#xff0c;如果您有一個測試案例可能一次失敗100次&#xff0c;或者像我的案例一樣在AWTM…

Gunicorn配置部分的翻譯

寫在前面&#xff0c;雖然翻譯得很爛&#xff0c;但也是我的勞動成果&#xff0c;轉載請注明出處&#xff0c;謝謝。 Gunicorn版本號19.7.1 Gunicorn配置 概述 三種配置方式 優先級如下&#xff0c;越后的優先級越大 1.框架的設置&#xff08;現在只有paster在用這個&#xff0…

臺式計算機風扇聲音大怎么處理,如何解決電腦電源風扇聲音大的問題?

現在的臺式機一般用3到5年后&#xff0c;一些問題自然也就慢慢表現出來了。很多網友在使用電腦過程中都有電腦風扇聲音大怎么辦的問題&#xff0c;電腦風扇聲音大就會讓人覺得使用電腦很不舒服&#xff0c;怎么辦好呢&#xff1f;出現重要的問題要如何解決好呢&#xff1f;現在…

jsp分頁功能

http://blog.csdn.net/xiazdong/article/details/6857515轉載于:https://www.cnblogs.com/Baronboy/p/6112403.html

Spring Security第1部分–具有數據庫的簡單登錄應用程序

什么是Spring Security&#xff1f; Spring Security是一個提供安全解決方案的框架&#xff0c;可在Web請求級別和方法級別上處理身份驗證和授權。 Spring安全性通過兩種方式處理安全性。 一種是安全的Web請求&#xff0c;另一種是在URL級別限制訪問。 Spring Security使用Serv…