題目:構建一個二叉樹,並給定一個二叉樹根節點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;
}