劍指Offer之Java算法習(xí)題精講二叉樹與N叉樹
題目一
?解法
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { StringBuffer sb = new StringBuffer(); List<String> list = new ArrayList<String>(); public List<String> binaryTreePaths(TreeNode root) { method(root); return list; } public void method(TreeNode root){ if(root==null) return; int t = sb.length(); sb.append(root.val); if(root.left==null&&root.right==null){ list.add(sb.toString()); } sb.append("->"); method(root.left); method(root.right); sb.delete(t, sb.length()); } }
題目二
?解法
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { int ans = 0; public int sumOfLeftLeaves(TreeNode root) { method(root,false); return ans; } public void method(TreeNode root,boolean flag){ if(root==null) return; if(root.left==null&&root.right==null&&flag){ ans+=root.val; return; } method(root.left,true); method(root.right,false); } }
題目三
?解法
/* // Definition for a Node. class Node { public int val; public List<Node> children; public Node() {} public Node(int _val) { val = _val; } public Node(int _val, List<Node> _children) { val = _val; children = _children; } }; */ class Solution { public int maxDepth(Node root) { if(root==null){ return 0; } int maxChildDepth = 0; for(int i = 0;i<root.children.size();i++){ int childDepth = maxDepth(root.children.get(i)); maxChildDepth = Math.max(maxChildDepth, childDepth); } return maxChildDepth+1; } }
到此這篇關(guān)于劍指Offer之Java算法習(xí)題精講二叉樹與N叉樹的文章就介紹到這了,更多相關(guān) Java 二叉樹內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
一不小心就讓Java開發(fā)踩坑的fail-fast是個(gè)什么鬼?(推薦)
這篇文章主要介紹了Java fail-fast,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04MyBatis直接執(zhí)行SQL的工具SqlMapper
今天小編就為大家分享一篇關(guān)于MyBatis直接執(zhí)行SQL的工具SqlMapper,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12Java代碼注釋規(guī)范(動力節(jié)點(diǎn)整理)
代碼注釋是架起程序設(shè)計(jì)者與程序閱讀者之間的通信橋梁,最大限度的提高團(tuán)隊(duì)開發(fā)合作效率。也是程序代碼可維護(hù)性的重要環(huán)節(jié)之一。下面通過本文說一下我們在日常開發(fā)中使用的代碼注釋規(guī)范2017-03-03使用Springboot整合GridFS實(shí)現(xiàn)文件操作
這篇文章主要介紹了使用Springboot整合GridFS實(shí)現(xiàn)文件操作,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10詳解通過maven運(yùn)行項(xiàng)目的兩種方式
這篇文章主要介紹了通過maven運(yùn)行項(xiàng)目的兩種方式,給大家提到了通過tomcat的方式來啟動maven項(xiàng)目的方法,通過圖文并茂的形式給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-12-12java導(dǎo)出包含多個(gè)sheet的Excel代碼示例
這篇文章主要介紹了java導(dǎo)出包含多個(gè)sheet的Excel,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03Java多線程Callable接口實(shí)現(xiàn)代碼示例
相信大家對Java編程中如何創(chuàng)建線程已經(jīng)不陌生了,這篇文章就向朋友們介紹實(shí)現(xiàn)callable接口,具體實(shí)例詳見正文。2017-10-10