当前位置:   article > 正文

树与二叉树

树与二叉树

前言:

树这个结构想必在日常生活中很常见到,而现在要介绍的是一种独特的数据结构--二叉树。

1.树

(1)定义:

是一种非线性结构,有一个特殊的节点叫做根节点,树没有前驱节点;树是递归定义的;树形结构中子树不能有交集,否则就不是树形结构。

(2)树相关重要概念:(如上图)

节点的度:一个节点含有的子树的个数。eg:A的度为2,B的度为3。

树的度:一棵树中,所有节点的度中的最大值。 eg:树的度为3.

叶子节点/终端节点:节点的度为0的节点。eg:叶子节点:E,D,G,F

双亲节点/父节点:若有一个节点含有子节点,则称这个节点为该子节点的双亲节点/父节点。

孩子节点/子节点:一个节点含有的子树的根节点称为该节点的子节点。

根节点:一颗树中,没有双亲节点的节点。

节点的层次:从根节点开始,根为第一层,根的子节点为第二层,以此类推。

树的高度/深度:树中节点的最大层次。eg:树的深度/高度为3.

2.二叉树

(1)定义:

该树中每个节点的度都小于等于2;且子树有左右子树之分。下图都是二叉树。

(2)两种特殊的二叉树:

a.完全二叉树:如果一颗树的节点个数为n,深度为K,且树中每一个节点都是深度为K的满二叉树中编号从0~(n-1)的节点,则称为完全二叉树。

b.满二叉树:每层节点数都是最大值 => 如果一颗树的高度为n,且树节点的总数为2^n-1,则称为满二叉树。

(3)二叉树的性质:

a.若根节点的层数为1,则一颗非空二叉树的第i层上的节点最多有2^{i-1}个。

b.若规定只有根节点的二叉树的深度为1,则深度为K的二叉树节点总数最多为2^{k}-1

c.对任何一颗二叉树,如果叶子节点的个数为n_0{},节点的度为2的节点个数为n_2{},则n_2{}+1=n_0{}

d.具有n个节点的完全二叉树,则该树的深度为\log_{2}\left ( n+1 \right )向上取整。

e.对于具有n个节点的完全二叉树,如果按照从上到下,从左到右的顺序对所有节点,从0开始编号,对于第i个节点有:

如果i>0,则双亲节点/父节点:\frac{i-1}{2};如果i=0,则没有双亲节点/父节点;

如果2i+1<n,则该节点有左孩子;否则没有左孩子。

如果2i+2<n,则该节点有右孩子;否则没有右孩子。

 f.对于一颗完全二叉树来说,如果该树节点总数为偶数个,则节点的度为1的节点只有一个;若为奇数个,则没有节点的度为1的节点。

(4)二叉树的遍历及其代码实现:

在实现二叉树的遍历之前,首先需要创建一个类TreeNode(二叉树的节点,包含该节点的值,该节点的左孩子节点,该节点的右孩子节点),所以:

  1. class TreeNode {
  2. public TreeNode left;//左孩子
  3. public TreeNode right;//右孩子
  4. public int val;
  5. public TreeNode(int val) {
  6. this.val = val;
  7. }
  8. }

a.前序遍历:根节点 -> 左子树 -> 右子树

分析:

代码实现:

  1. public void preorderTraversal(TreeNode root) {
  2. if(root == null) {
  3. return;
  4. }
  5. System.out.print(root.val + " ");
  6. preorderTraversal(root.left);
  7. preorderTraversal(root.right);
  8. }

b.中序遍历:左子树 -> 根节点 -> 右子树

分析:

代码实现:

  1. public void inorderTraversal(TreeNode root) {
  2. if(root == null) {
  3. return;
  4. }
  5. preorderTraversal(root.left);
  6. System.out.print(root.val + " ");
  7. preorderTraversal(root.right);
  8. }

c.后序遍历:左子树 -> 右子树 -> 根节点

分析:

代码实现:

  1. public void lastorderTraversal(TreeNode root) {
  2. if(root == null) {
  3. return;
  4. }
  5. preorderTraversal(root.left);
  6. preorderTraversal(root.right);
  7. System.out.print(root.val + " ");
  8. }

(5)二叉树相关OJ题:

二叉树所有的题都会涉及到递归。所有OJ题的前提都是已经创建好了一颗树,并且知道根节点为root。

a.求树的高度。

题析:

题解:

  1. public int getHeight(TreeNode root) {
  2. if(root == null) {
  3. return 0;
  4. }
  5. int leftHeight = getHeight(root.left);
  6. int rightHeight = getHeight(root.right);
  7. return Math.max(leftHeight,rightHeight) + 1;//这里也可以用条件运算符
  8. }

b.求树中叶子节点的个数。

题析:

 题解:

  1. public int getLeafNodeCount(TreeNode root) {
  2. if(root == null) {
  3. return 0;
  4. }
  5. if(root.left == null && root.right == null) {
  6. return 1;
  7. }
  8. return getLeafNodeCount(root.left) + getLeafNodeCount(root.right);
  9. }

c.第i层有多少个节点。

题析:

题解:

  1. public int getKLevelNodeCount(TreeNode root, int k) {
  2. if(root == null) {
  3. return 0;
  4. }
  5. if(k == 1) {
  6. return 1;
  7. }
  8. return getKLevelNodeCount(root.left, k - 1) + getKLevelNodeCount(root.right, k - 1);
  9. }

 d.判断某个val值是否存在于树中。

题析:

题解:

  1. public TreeNode getVal(TreeNode root, int val) {
  2. if(root == null) {
  3. return null;
  4. }
  5. if(root.val == val) {
  6. return root;
  7. }
  8. TreeNode leftTree = getVal(root.left,val);
  9. if(leftTree != null) {
  10. return leftTree;
  11. }
  12. TreeNode rightTree = getVal(root.right,val);
  13. if(rightTree != null) {
  14. return rightTree;
  15. }
  16. return null;
  17. }

e.节点的总个数。

题析:

题解:

  1. public int size(TreeNode root) {
  2. if(root == null) {
  3. return 0;
  4. }
  5. return size(root.left) + size(root.right) + 1;
  6. }

 f.检查两棵树是否相同

题析:

题解:

  1. class Solution {
  2. public boolean isSameTree(TreeNode p, TreeNode q) {
  3. if(p == null && q == null) {
  4. return true;
  5. }
  6. if(p == null && q != null || q == null && p != null) {
  7. return false;
  8. }
  9. if(p.val != q.val) {
  10. return false;
  11. }
  12. return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
  13. }
  14. }

 g.另一棵树的子树

题析:

题解:

  1. class Solution {
  2. //判断两棵树的结构是否相同
  3. private boolean isSameTree(TreeNode p, TreeNode q) {
  4. if(p == null && q == null) {
  5. return true;
  6. }
  7. if(p == null && q != null || q == null && p != null) {
  8. return false;
  9. }
  10. if(p.val != q.val) {
  11. return false;
  12. }
  13. return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
  14. }
  15. public boolean isSubtree(TreeNode root, TreeNode subRoot) {
  16. if(root == null) {
  17. return false;
  18. }
  19. if(isSameTree(root,subRoot)) {
  20. return true;
  21. }
  22. return isSubtree(root.left,subRoot) || isSubtree(root.right,subRoot);
  23. }
  24. }

 h.翻转二叉树

题析:

题解:

法一:该种方法没有利用返回值(先序遍历)。

  1. class Solution {
  2. public TreeNode invertTree(TreeNode root) {
  3. if(root == null) {
  4. return null;
  5. }
  6. if(root.left == null && root.right == null) {
  7. return root;
  8. }
  9. TreeNode tmp = root.left;
  10. root.left = root.right;
  11. root.right = tmp;
  12. invertTree(root.left);
  13. invertTree(root.right);
  14. return root;
  15. }
  16. }

 法二:利用返回值(后序遍历)。

  1. class Solution {
  2. public TreeNode invertTree(TreeNode root) {
  3. if(root == null) {
  4. return null;
  5. }
  6. if(root.left == null && root.right == null) {
  7. return root;
  8. }
  9. TreeNode leftNode = invertTree(root.left);
  10. TreeNode rightNode = invertTree(root.right);
  11. root.left = rightNode;
  12. root.right = leftNode;
  13. return root;
  14. }
  15. }

i.是否为平衡二叉树

题析:

题解:

法一:该方法的时间复杂度为O\left ( n^{2} \right )

  1. class Solution {
  2. public boolean isBalanced(TreeNode root) {
  3. if(root == null) {
  4. return true;
  5. }
  6. int leftHeight = getHeight(root.left);
  7. int rightHeight = getHeight(root.right);
  8. if(Math.abs(leftHeight - rightHeight) > 1) {
  9. return false;
  10. }
  11. return isBalanced(root.left) && isBalanced(root.right);
  12. }
  13. private int getHeight(TreeNode root) {
  14. if(root == null) {
  15. return 0;
  16. }
  17. int leftHigh = getHeight(root.left);
  18. int rightHigh = getHeight(root.right);
  19. return Math.max(leftHigh,rightHigh) + 1;
  20. }
  21. }

 法二:可以实现时间复杂度为O\left ( n\right ),只需要去修改getHeight方法,在求高度的过程中去判断是否每一个节点的高度差 <= 1。

题解:

  1. class Solution {
  2. public boolean isBalanced(TreeNode root) {
  3. if(root == null) {
  4. return true;
  5. }
  6. return getHeight(root) >= 0;
  7. }
  8. private int getHeight(TreeNode root) {
  9. if(root == null) {
  10. return 0;
  11. }
  12. int leftHigh = getHeight(root.left);
  13. if(leftHigh == -1) {
  14. return -1;
  15. }
  16. int rightHigh = getHeight(root.right);
  17. if(rightHigh >= 0 && Math.abs(leftHigh - rightHigh) <= 1) {
  18. return Math.max(leftHigh,rightHigh) + 1;
  19. }
  20. return -1;
  21. }
  22. }

j.二叉树的构建与遍历

题析:

题解:

  1. import java.util.Scanner;
  2. class TreeNode {
  3. public char val;
  4. public TreeNode left;
  5. public TreeNode right;
  6. public TreeNode(char val) {
  7. this.val = val;
  8. }
  9. }
  10. public class Main {
  11. private static int i;
  12. //注意static修饰的变量,每次调用这个方法的时候都是对同一个变量进行修改的。
  13. //所以第二次调用该方法的时候,i不为0.所以需要在while循环中将i置0/使用非静态方法
  14. private static TreeNode createTree(String str) {
  15. TreeNode root = null;
  16. if (str.charAt(i) != '#') {
  17. root = new TreeNode(str.charAt(i));
  18. i++;
  19. root.left = createTree(str);
  20. root.right = createTree(str);
  21. }else {
  22. i++;
  23. }
  24. return root;
  25. }
  26. //中序遍历
  27. private static void inOrder(TreeNode root) {
  28. if(root == null) {
  29. return;
  30. }
  31. inOrder(root.left);
  32. System.out.print(root.val + " ");
  33. inOrder(root.right);
  34. }
  35. public static void main(String[] args) {
  36. Scanner in = new Scanner(System.in);
  37. // 注意 hasNext 和 hasNextLine 的区别
  38. while (in.hasNextLine()) { // 注意 while 处理多个 case
  39. String str = in.nextLine();
  40. i = 0;
  41. TreeNode root = createTree(str);
  42. inOrder(root);
  43. }
  44. }
  45. }

 k.对称二叉树

题析:

题解:

  1. class Solution {
  2. private boolean isSymmetricChild(TreeNode leftNode, TreeNode rightNode) {
  3. if(leftNode == null && rightNode != null || rightNode == null && leftNode != null){
  4. return false;
  5. }
  6. if(leftNode == null && rightNode == null) {
  7. return true;
  8. }
  9. if(leftNode.val != rightNode.val) {
  10. return false;
  11. }
  12. return isSymmetricChild(leftNode.left,rightNode.right) &&
  13. isSymmetricChild(leftNode.right,rightNode.left);
  14. }
  15. public boolean isSymmetric(TreeNode root) {
  16. return isSymmetricChild(root.left, root.right);
  17. }
  18. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/在线问答5/article/detail/836286
推荐阅读
相关标签
  

闽ICP备14008679号