解法
/** * 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.r腊肠炒什么菜好吃ight==null){ list.add(sb.tostri关于六一的手抄报图片ng()); } 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,fal); 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.l原料采购合同eft,true); method(root.right,fal); }}
解法
/*// 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 maxc10248hilddepth+1; }}
到此这篇关于剑指offer之java算法习题精讲二叉树与n叉树的文章就介绍到这了,更多相关 java 二叉树内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!
本文发布于:2023-04-06 04:40:50,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/bc0e03cca97f41f62d746259d64faee8.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:剑指Offer之Java算法习题精讲二叉树与N叉树.doc
本文 PDF 下载地址:剑指Offer之Java算法习题精讲二叉树与N叉树.pdf
留言与评论(共有 0 条评论) |