哪个网站是专门做装修的,选择网站设计公司佛山,建网站空间可以不买,网页无法访问此页面1.前言
http://t.csdnimg.cn/lO4S7
在前文我们已经简单的讲解了二叉树的基本概念#xff0c;本文将讲解具体的实现
2.基本功能的实现
2.1获取树中节点个数 public int size(TreeNode root){if(rootnull){return 0;}int retsize(root.left)size(root.right)1;return ret;}p…1.前言
http://t.csdnimg.cn/lO4S7
在前文我们已经简单的讲解了二叉树的基本概念本文将讲解具体的实现
2.基本功能的实现
2.1获取树中节点个数 public int size(TreeNode root){if(rootnull){return 0;}int retsize(root.left)size(root.right)1;return ret;}public static int nodeSize;public void size2(TreeNode root){if(rootnull){return;}nodeSize;size2(root.left);size2(root.right);}
2.2获取叶子节点的个数 /*** 求叶子节点个数* param root* return*/public int getLeafNodeCount(TreeNode root){if(rootnull){return 0;}if(root.leftnullroot.rightnull){return 1;}return getLeafNodeCount(root.left)getLeafNodeCount(root.right);}public int leftSize;public void getLeafNodeCount2(TreeNode root){if(rootnull){return;}if(root.leftnullroot.rightnull){leftSize;}getLeafNodeCount2(root.left);getLeafNodeCount2(root.right);}
2.3获取第K层节点的个数 /*第k层有几个节点*/public int getKLevelNodeCount(TreeNode root,int k){if(rootnull){return 0;}if(k1){return 1;}return getKLevelNodeCount(root.left,k-1)getKLevelNodeCount(root.right,k-1);}
2.4获取二叉树的高度 public int getHeight(TreeNode root){if(rootnull){return 0;}int leftHeightgetHeight(root.left);int rightHeightgetHeight(root.right);return leftHeightrightHeight?leftHeight1:rightHeight1;}
2.5检测值为value的元素是否存在 /**** param root* param val* return*/public TreeNode find(TreeNode root,char val){if(rootnull){return null;}if(root.valval){return root;}TreeNode retfind(root.left,val);if(ret!null){return ret;}retfind(root.right,val);if(ret!null){return ret;}return null;}
2.6判断一棵树是不是完全二叉树 public boolean isSameTree(TreeNode p,TreeNode q){if(p!nullqnull||pnullq!null){return false;}if(pnullqnull){return true;}if(p.val!q.val){return false;}return isSameTree(p.left,q.left)isSameTree(p.right,q.right);}
3.二叉树的应用
在知晓了二叉树相关功能的底层实现之后我们应用二叉树知识来解答
3.1二叉树遍历
二叉树遍历_牛客题霸_牛客网
根据题目我们可以知道这其实是两个题目一是创建二叉树二是中序遍历结果
所以我们分成两部来完成
先定义二叉树的结构 class TreeNode {public char val;public TreeNode left;public TreeNode right;public TreeNode(char val) {this.val val;}}
当不为#的时候我们就创建节点 public static int i 0;public static TreeNode createTree(String str) {TreeNode root null;if (str.charAt(i) ! #) {root new TreeNode(str.charAt(i));i;root.left createTree(str);root.right createTree(str);} else {i;}return root;}
最后我们进行中序遍历——根左子树-根节点-根右子树的顺序来递归即可 public static void inorder(TreeNode root){if(rootnull){return;}inorder(root.left);System.out.print(root.val );inorder(root.right);}
3.2二叉树的层序遍历
. - 力扣LeetCode 这题主要是对二叉树的层序遍历对于层序遍历是如何遍历的我们在上文已经讲过——从上而下自左向右依次遍历. 在了解到概念之后我们就可以进行解答了
我们要对知识进行灵活的应用这种从上而下自左向右的逻辑与前面所讲到的栈和队列知识中队列先进先出逻辑类似所以我们可以用创建队列来解答
1.是否为空若为空则返回空列表
2.若不为空则进入循环判断当队列为空则跳出循环创建一个列表来打印每层的结果
3.将每层结果存入ret,最后返回ret即可
/*** 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 ListListInteger levelOrder(TreeNode root) {ListListInteger retnew ArrayList();if(rootnull){return ret;}QueueTreeNode queue new LinkedList();queue.offer(root);while(!queue.isEmpty()){int sizequeue.size();ListInteger listnew ArrayList();//每层while(size0){TreeNode curqueue.poll();list.add(cur.val);if(cur.left!null){queue.offer(cur.left);}if(cur.right!null){queue.offer(cur.right);}size--;}ret.add(list);}return ret;}
} 对二叉树的基础知识讲解就到这里如果上述内容对您有帮助希望给个三连谢谢