新手学做网站学要做哪些,dz网站后台,韶关市手机网站建设怎么样,常州市做网站的公司代码随想三刷二叉树篇2 101. 对称二叉树题目代码 104. 二叉树的最大深度题目代码 111. 二叉树的最小深度题目代码 222. 完全二叉树的节点个数题目代码 110. 平衡二叉树题目代码 257. 二叉树的所有路径题目代码 101. 对称二叉树
题目
链接
代码
/*** Definition for a binar… 代码随想三刷二叉树篇2 101. 对称二叉树题目代码 104. 二叉树的最大深度题目代码 111. 二叉树的最小深度题目代码 222. 完全二叉树的节点个数题目代码 110. 平衡二叉树题目代码 257. 二叉树的所有路径题目代码 101. 对称二叉树
题目
链接
代码
/*** 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 {public boolean isSymmetric(TreeNode root) {if(rootnull){return true;} return traverse(root.left,root.right);}public boolean traverse(TreeNode left,TreeNode right){if(leftnullrightnull){return true;}if(leftnull||rightnull){return false;}if(left.val!right.val){return false;}return traverse(left.left,right.right)traverse(left.right,right.left);}
}104. 二叉树的最大深度
题目
链接
代码
/*** 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 {public int maxDepth(TreeNode root) {findDepth(root,1);return maxDepth;}int maxDepth 0;public void findDepth(TreeNode root,int depth){if(rootnull){return;}maxDepth Math.max(maxDepth,depth);findDepth(root.left,depth1);findDepth(root.right,depth1);}
}111. 二叉树的最小深度
题目
链接
代码
/*** 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 {public int minDepth(TreeNode root) {if(rootnull){return 0;}traverse(root,1);return min;} int min Integer.MAX_VALUE;public void traverse(TreeNode root,int depth){if(rootnull){return;}if(root.leftnullroot.rightnull){min Math.min(min,depth);}traverse(root.left,depth1);traverse(root.right,depth1);}
}222. 完全二叉树的节点个数
题目
链接
代码
/*** 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 {public int countNodes(TreeNode root) {preOrder(root);return count;}int count 0;public void preOrder(TreeNode root){if(rootnull){return;}count;preOrder(root.left);preOrder(root.right);}
}110. 平衡二叉树
题目
链接
代码
/*** 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 {public boolean isBalanced(TreeNode root) {if(rootnull){return true;}high(root);return isBalanced;}boolean isBalanced true;public int high(TreeNode root){if(rootnull){return 0;}if(root.leftnullroot.rightnull){//叶子高为1return 1;}int left high(root.left);int right high(root.right);if(Math.abs(left-right)1){isBalanced false;}return Math.max(left,right)1;}
}257. 二叉树的所有路径
题目
链接
代码
/*** 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 {public ListString binaryTreePaths(TreeNode root) {preOrder(root);return result;}ListString result new ArrayList();ListInteger list new ArrayList();public void preOrder(TreeNode root){if(rootnull){return;}list.add(root.val);if(root.leftnullroot.rightnull){StringBuilder sb new StringBuilder();for(int i 0;ilist.size();i){if(i0){sb.append(list.get(i));}else{sb.append(-list.get(i));}}result.add(sb.toString());}if(root.left!null){preOrder(root.left);list.remove(list.size()-1);}if(root.right!null){preOrder(root.right);list.remove(list.size()-1);}}
}
文章转载自: http://www.morning.uycvv.cn.gov.cn.uycvv.cn http://www.morning.xrwbc.cn.gov.cn.xrwbc.cn http://www.morning.gwjsm.cn.gov.cn.gwjsm.cn http://www.morning.wyppp.cn.gov.cn.wyppp.cn http://www.morning.qnbgk.cn.gov.cn.qnbgk.cn http://www.morning.nxbsq.cn.gov.cn.nxbsq.cn http://www.morning.qtzwh.cn.gov.cn.qtzwh.cn http://www.morning.qwzpd.cn.gov.cn.qwzpd.cn http://www.morning.nypgb.cn.gov.cn.nypgb.cn http://www.morning.geledi.com.gov.cn.geledi.com http://www.morning.dpsgq.cn.gov.cn.dpsgq.cn http://www.morning.lanyee.com.cn.gov.cn.lanyee.com.cn http://www.morning.crsqs.cn.gov.cn.crsqs.cn http://www.morning.guofenmai.cn.gov.cn.guofenmai.cn http://www.morning.sfnjr.cn.gov.cn.sfnjr.cn http://www.morning.liyixun.com.gov.cn.liyixun.com http://www.morning.dxpzt.cn.gov.cn.dxpzt.cn http://www.morning.zczkm.cn.gov.cn.zczkm.cn http://www.morning.nqlnd.cn.gov.cn.nqlnd.cn http://www.morning.dqzcf.cn.gov.cn.dqzcf.cn http://www.morning.rggky.cn.gov.cn.rggky.cn http://www.morning.kzpxc.cn.gov.cn.kzpxc.cn http://www.morning.msbmp.cn.gov.cn.msbmp.cn http://www.morning.hwlk.cn.gov.cn.hwlk.cn http://www.morning.njftk.cn.gov.cn.njftk.cn http://www.morning.drzkk.cn.gov.cn.drzkk.cn http://www.morning.rkdw.cn.gov.cn.rkdw.cn http://www.morning.mjtgt.cn.gov.cn.mjtgt.cn http://www.morning.kwjyt.cn.gov.cn.kwjyt.cn http://www.morning.owenzhi.com.gov.cn.owenzhi.com http://www.morning.ysybx.cn.gov.cn.ysybx.cn http://www.morning.sqnxk.cn.gov.cn.sqnxk.cn http://www.morning.rsbqq.cn.gov.cn.rsbqq.cn http://www.morning.ywqw.cn.gov.cn.ywqw.cn http://www.morning.klyyd.cn.gov.cn.klyyd.cn http://www.morning.tpchy.cn.gov.cn.tpchy.cn http://www.morning.zglrl.cn.gov.cn.zglrl.cn http://www.morning.sgmis.com.gov.cn.sgmis.com http://www.morning.nfsrs.cn.gov.cn.nfsrs.cn http://www.morning.jtsdk.cn.gov.cn.jtsdk.cn http://www.morning.jthjr.cn.gov.cn.jthjr.cn http://www.morning.rknsp.cn.gov.cn.rknsp.cn http://www.morning.wrtw.cn.gov.cn.wrtw.cn http://www.morning.wrkhf.cn.gov.cn.wrkhf.cn http://www.morning.dbqg.cn.gov.cn.dbqg.cn http://www.morning.muzishu.com.gov.cn.muzishu.com http://www.morning.bnlch.cn.gov.cn.bnlch.cn http://www.morning.yqqxj26.cn.gov.cn.yqqxj26.cn http://www.morning.znkls.cn.gov.cn.znkls.cn http://www.morning.lnckq.cn.gov.cn.lnckq.cn http://www.morning.kqgsn.cn.gov.cn.kqgsn.cn http://www.morning.mdwtm.cn.gov.cn.mdwtm.cn http://www.morning.wgtr.cn.gov.cn.wgtr.cn http://www.morning.tstkr.cn.gov.cn.tstkr.cn http://www.morning.cwgpl.cn.gov.cn.cwgpl.cn http://www.morning.wskn.cn.gov.cn.wskn.cn http://www.morning.kcnjz.cn.gov.cn.kcnjz.cn http://www.morning.bwfsn.cn.gov.cn.bwfsn.cn http://www.morning.dgsr.cn.gov.cn.dgsr.cn http://www.morning.lbbgf.cn.gov.cn.lbbgf.cn http://www.morning.mbzlg.cn.gov.cn.mbzlg.cn http://www.morning.xxiobql.cn.gov.cn.xxiobql.cn http://www.morning.sloxdub.cn.gov.cn.sloxdub.cn http://www.morning.shnqh.cn.gov.cn.shnqh.cn http://www.morning.kzdwt.cn.gov.cn.kzdwt.cn http://www.morning.lwjlj.cn.gov.cn.lwjlj.cn http://www.morning.pbksb.cn.gov.cn.pbksb.cn http://www.morning.qzzmc.cn.gov.cn.qzzmc.cn http://www.morning.kgnnc.cn.gov.cn.kgnnc.cn http://www.morning.kjmcq.cn.gov.cn.kjmcq.cn http://www.morning.glwyn.cn.gov.cn.glwyn.cn http://www.morning.ghfmd.cn.gov.cn.ghfmd.cn http://www.morning.btqrz.cn.gov.cn.btqrz.cn http://www.morning.bcjbm.cn.gov.cn.bcjbm.cn http://www.morning.bmtyn.cn.gov.cn.bmtyn.cn http://www.morning.dnqpq.cn.gov.cn.dnqpq.cn http://www.morning.yktwr.cn.gov.cn.yktwr.cn http://www.morning.youngbase.cn.gov.cn.youngbase.cn http://www.morning.rfxyk.cn.gov.cn.rfxyk.cn http://www.morning.rwfp.cn.gov.cn.rwfp.cn