mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-10 14:36:06 +00:00
Add missing Dart codes and fix some errors (#689)
* Add missing Dart codes and fix some errors * Update array_binary_tree.dart --------- Co-authored-by: Yudong Jin <krahets@163.com>
This commit is contained in:
@@ -4,19 +4,12 @@
|
||||
* Author: Jefferson (JeffersonHuang77@gmail.com)
|
||||
*/
|
||||
|
||||
/**
|
||||
* Definition for a singly-linked list node
|
||||
*/
|
||||
/* Definition for a singly-linked list node */
|
||||
class ListNode {
|
||||
int val;
|
||||
ListNode? next;
|
||||
|
||||
ListNode(this.val, [this.next]);
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'ListNode{val: $val, next: $next}';
|
||||
}
|
||||
}
|
||||
|
||||
/* Generate a linked list with a vector */
|
||||
|
||||
@@ -16,6 +16,7 @@ class Trunk {
|
||||
Trunk(this.prev, this.str);
|
||||
}
|
||||
|
||||
/* Print a matrix (Array) */
|
||||
void printMatrix(List<List<int>> matrix) {
|
||||
print("[");
|
||||
for (List<int> row in matrix) {
|
||||
@@ -24,6 +25,7 @@ void printMatrix(List<List<int>> matrix) {
|
||||
print("]");
|
||||
}
|
||||
|
||||
/* Print a linked list */
|
||||
void printLinkedList(ListNode? head) {
|
||||
List<String> list = [];
|
||||
|
||||
@@ -35,6 +37,11 @@ void printLinkedList(ListNode? head) {
|
||||
print(list.join(' -> '));
|
||||
}
|
||||
|
||||
/**
|
||||
* The interface of the tree printer
|
||||
* This tree printer is borrowed from TECHIE DELIGHT
|
||||
* https://www.techiedelight.com/c-program-print-binary-tree/
|
||||
*/
|
||||
void printTree(TreeNode? root, [Trunk? prev = null, bool isLeft = false]) {
|
||||
if (root == null) {
|
||||
return;
|
||||
@@ -65,6 +72,7 @@ void printTree(TreeNode? root, [Trunk? prev = null, bool isLeft = false]) {
|
||||
printTree(root.left, trunk, false);
|
||||
}
|
||||
|
||||
/* Helper function to print branches of the binary tree */
|
||||
void showTrunks(Trunk? p) {
|
||||
if (p == null) {
|
||||
return;
|
||||
@@ -74,6 +82,7 @@ void showTrunks(Trunk? p) {
|
||||
stdout.write(p.str);
|
||||
}
|
||||
|
||||
/* Print a heap (PriorityQueue) */
|
||||
void printHeap(List<int> heap) {
|
||||
print("堆的数组表示:$heap");
|
||||
print("堆的树状表示:");
|
||||
|
||||
@@ -4,63 +4,47 @@
|
||||
* Author: Jefferson (JeffersonHuang77@gmail.com)
|
||||
*/
|
||||
|
||||
import 'dart:collection';
|
||||
|
||||
/* 二叉树节点类 */
|
||||
class TreeNode {
|
||||
int val; // 节点值
|
||||
int height; // 节点高度
|
||||
TreeNode? left; // 左子节点引用
|
||||
TreeNode? right; // 右子节点引用
|
||||
|
||||
/* 构造方法 */
|
||||
TreeNode(this.val, [this.height = 0, this.left, this.right]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a binary tree given an array
|
||||
* @param list
|
||||
* @return
|
||||
*/
|
||||
TreeNode? listToTree(List<int> list) {
|
||||
int size = list.length;
|
||||
if (size == 0) return null;
|
||||
|
||||
TreeNode root = TreeNode(list[0]);
|
||||
Queue<TreeNode?> queue = Queue();
|
||||
queue.add(root);
|
||||
int i = 0;
|
||||
while (queue.isNotEmpty) {
|
||||
TreeNode? node = queue.removeFirst();
|
||||
if (++i >= size) break;
|
||||
node?.left = TreeNode(list[i]);
|
||||
queue.add(node?.left);
|
||||
if (++i >= size) break;
|
||||
node?.right = TreeNode(list[i]);
|
||||
queue.add(node?.right);
|
||||
/* 将列表反序列化为二叉树:递归 */
|
||||
TreeNode? listToTreeDFS(List<int?> arr, int i) {
|
||||
if (i < 0 || i >= arr.length || arr[i] == null) {
|
||||
return null;
|
||||
}
|
||||
TreeNode? root = TreeNode(arr[i]!);
|
||||
root.left = listToTreeDFS(arr, 2 * i + 1);
|
||||
root.right = listToTreeDFS(arr, 2 * i + 2);
|
||||
return root;
|
||||
}
|
||||
|
||||
/**
|
||||
* Serialize a binary tree to a list
|
||||
* @param root
|
||||
* @return
|
||||
*/
|
||||
List<int?> treeToList(TreeNode? root) {
|
||||
List<int?> list = [];
|
||||
if (root == null) return list;
|
||||
Queue<TreeNode?> queue = Queue();
|
||||
queue.add(root);
|
||||
|
||||
while (!queue.isEmpty) {
|
||||
TreeNode? node = queue.first;
|
||||
queue.removeFirst();
|
||||
if (node != null) {
|
||||
list.add(node.val);
|
||||
queue.add(node.left);
|
||||
queue.add(node.right);
|
||||
} else {
|
||||
list.add(null);
|
||||
}
|
||||
}
|
||||
return list;
|
||||
/* 将列表反序列化为二叉树 */
|
||||
TreeNode? listToTree(List<int?> arr) {
|
||||
return listToTreeDFS(arr, 0);
|
||||
}
|
||||
|
||||
/* 将二叉树序列化为列表:递归 */
|
||||
void treeToListDFS(TreeNode? root, int i, List<int?> res) {
|
||||
if (root == null) return;
|
||||
while (i >= res.length) {
|
||||
res.add(null);
|
||||
}
|
||||
res[i] = root.val;
|
||||
treeToListDFS(root.left, 2 * i + 1, res);
|
||||
treeToListDFS(root.right, 2 * i + 2, res);
|
||||
}
|
||||
|
||||
/* 将二叉树序列化为列表 */
|
||||
List<int?> treeToList(TreeNode? root) {
|
||||
List<int?> res = [];
|
||||
treeToListDFS(root, 0, res);
|
||||
return res;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user