面试被虐系列_算法分析篇_二叉树
来源:互联网 发布:数据恢复大师破解版 编辑:程序博客网 时间:2024/05/23 19:16
题目:构建一个二叉树,并给定一个二叉树根节点Node,请写出函数计算出二叉树的深度,叶子节点。(兼论二叉树的不同遍历方式)
思考:
在数据结构中,树是一类非常重要的结构,很多底层非常重要的基础算法都是建立在树的基础上的。数据库索引的B-tree结构存储,路由搜索引擎的搜索算法,文件系统等等。二叉树又是树中一种非常重要,又应用广泛的结构。这里重点阐述二叉树的一个相关算法,以后如果有机会再深入聊聊树相关的其他方面。
这里遇到的一个很典型的问题,就是计算二叉树的深度和叶子节点数。这里我使用几种常用的语言进行实现,同时也简单聊聊二叉树的几种不同遍历方式。结果参考网络上一些前辈的文字,以及结合个人面试过程中的一些细节。欢迎大家吐槽,交流心得。
C#实现树深度和叶子节点计算:
/*****递归计算二叉树的深度和叶子节点数*****/ public class TreeNodeHelper { public static Node CreateTree() { Node root = new Node(); root.data = 9; Node temp01 = new Node(); temp01.data = 1; root.left = temp01; Node temp02 = new Node(); temp02.data = 3; root.right = temp02; Node temp03 = new Node(); temp03.data = 2; root.left.left = temp03; Node temp04 = new Node(); temp04.data = 4; root.left.right = temp04; return root; } // 叶子数 public static int leafNum(Node node) { if (node != null) { if (node.left == null && node.right == null) { return 1; } return leafNum(node.left) + leafNum(node.right); } return 0; } // 求二叉树的深度 public static int deep(Node node) { int h1, h2; if (node == null) { return 0; } else { h1 = deep(node.left); h2 = deep(node.right); return (h1 < h2) ? h2 + 1 : h1 + 1; } } // 中序遍历 左子树->根节点->右子树 public static void SelectTreeIn(Node root) { if (root == null) return; SelectTreeIn(root.left); Console.WriteLine(root.data + " "); SelectTreeIn(root.right); } // 先序遍历 根节点->左子树->右子树 public static void SelectTreePre(Node root) { if (root == null) return; Console.WriteLine(root.data + " "); SelectTreePre(root.left); SelectTreePre(root.right); } // 后序遍历 左子树->右子树->根节点 public static void SelectTreePost(Node root) { if (root == null) return; SelectTreePost(root.left); SelectTreePost(root.right); Console.WriteLine(root.data + " "); } // public class Node { bool visited = false; public int data = 0; public Node left = null; public Node right = null; }
JAVA实现树深度和叶子节点计算:
public class TreeNodeHelper { public static Node CreateTree() { Node root = new Node(); root.data = 9; Node temp01 = new Node(); temp01.data = 1; root.left = temp01; Node temp02 = new Node(); temp02.data = 3; root.right = temp02; Node temp03 = new Node(); temp03.data = 2; root.left.left = temp03; Node temp04 = new Node(); temp04.data = 4; root.left.right = temp04; return root; } // 叶子数 public static int leafNum(Node node) { if (node != null) { if (node.left == null && node.right == null) { return 1; } return leafNum(node.left) + leafNum(node.right); } return 0; } // 求二叉树的深度 public static int deep(Node node) { int h1, h2; if (node == null) { return 0; } else { h1 = deep(node.left); h2 = deep(node.right); return (h1 < h2) ? h2 + 1 : h1 + 1; } } // 中序遍历 左子树->根节点->右子树 public static void SelectTreeIn(Node root) { if (root == null) return; SelectTreeIn(root.left); System.out.printf(root.data + " "); SelectTreeIn(root.right); } // 先序遍历 根节点->左子树->右子树 public static void SelectTreePre(Node root) { if (root == null) return; System.out.printf(root.data + " "); SelectTreePre(root.left); SelectTreePre(root.right); } // 后序遍历 左子树->右子树->根节点 public static void SelectTreePost(Node root) { if (root == null) return; SelectTreePost(root.left); SelectTreePost(root.right); System.out.printf(root.data + " "); } // public class Node { boolean visited = false; int data = 0; Node left = null; Node right = null; }
0 0
- 面试被虐系列_算法分析篇_二叉树
- 面试被虐系列_算法分析篇_排序算法
- 面试被虐系列_前端开发篇_Ajax
- 面试被虐系列_设计模式篇_单例模式
- 算法_二叉树反转
- 二、算法_二叉树
- 数据结构_二叉树_遍历算法应用
- java版本_二叉树_非递归算法实现
- 每日算法_编程之美_重建二叉树
- 算法6_二叉树遍历
- 数据结构与算法_二叉查找树
- 面试被虐系列_基础数据结构篇_C#集合接口_2
- 面试被虐系列_基础数据结构篇_C#集合接口_3
- 算法系列_基础02_树的基本概念
- 算法课_算法分析_
- 直角三角形_算法分析
- 02_算法分析
- 数据结构_二叉树
- 仿新浪微博加#话题的EditText实现
- spingboot jpa String 映射mysql为 LongText类型
- 算法15:打印中两个整数范围内的所有素数,例如:(12, 15) ->13
- 操作手机联系人
- QT修改qss文件后导致样式表不生效
- 面试被虐系列_算法分析篇_二叉树
- zookeeper小例子
- The Process of Submitting a Job
- 反向传导算法
- JAVA中的null
- 微服务技能树
- java中的synchronized(同步代码块和同步方法的区别)
- 数据源
- SmartGit 破解